zoukankan      html  css  js  c++  java
  • 嵌入式培训学习历程第二十四天

                标准库的三部分

      1.string

      2.标准I/O

      3.kernel内核

      4.小技巧 (查看你已经写的代码量 )  命令:find ./代码所在文件夹/ -name "*.c" | xargs wc -l

      

      1.标准I/O :主存与外部设备的数据交换

      2.文本文件 : 最早的定义(都可见的ASCII组成的) 

       二进制文件 : 人看不懂的。。 

        区别 : 文本文件可见, 二进制文件不可见。

       以上只限于标准I/O里面

      3.UTF-8 :占三个字节

      4. sizeof  计算类型大小  在编译阶段

       strlen   以 ‘’为结尾  在执行阶段

      5.操作文件分为三步 :

    fopen FILE *fopen(const char *path, const char*moe);
    r/w/a r, r+, w, w+, a, a+
    fclose 成功返回0,其它返回EOF
    打开方式 含义
    r 只读,文件必须已存在
    r+ 允许读和写,文件必须已存在
    w 只写,如果文件不存在则创建,如果文件已存在则把文件长度截断为0字节再重新写,也就是替换掉原来的文件内容
    w+ 允许读和写,如果文件不存在则创建,如果文件已存在则把文件长度截断为0字节再重新写
    a 只能在文件末尾追加数据, 如果文件不存在则创建
    a+ 允许读和追加数据,如果文件不存在则创建

       6.FILE *fp;    文件句柄。成功返回文件指针,失败返回NULL。

      7.strerror:  errno

      8.fgetc/fputc  : 以字符为单位,只针对文本文件

      9.文件结尾 : length长度达到

      10.资源泄漏主要针对长期运行的服务器等。。

      11.

    文件指针   默认的含义
    stdin 标准输入 键盘
    stdout 标准输出 屏幕
    stderr 标准错误 屏幕

      13.goto语句的典型用法 :为了将已打开的文件关闭

     1 #include <stdio.h>
     2 #include <errno.h>
     3 #include <string.h>
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     if(argc < 3) {
     8         fprintf(stderr,"Please input %s <file1> <file2>
    ", argv[0]);
     9         return 1;
    10     }
    11 
    12     FILE *fp_output, *fp_input;
    13 
    14     if(NULL == (fp_output = fopen(argv[1], "r"))) {
    15         printf("The %s<file> is Erro: %s .
    ", argv[1], strerror(errno));
    16         return 1;
    17     }
    18     if(NULL == (fp_input = fopen(argv[2], "w+"))) {
    19         printf("The %s<file> is Erro: %s .
    ", argv[2], strerror(errno));
    20         goto err;
    21     }
    22     
    23     fseek(fp_output, 0, SEEK_END);
    24     int len = ftell(fp_output);
    25     fseek(fp_input, len -1, SEEK_SET);
    26     int ch_error;
    27     if(EOF == (ch_error = fputc('0', fp_input))) {
    28         printf("The <file>%s is copy Erro.
    ", strerror(errno));
    29         goto err1;
    30     }
    31 
    32     rewind(fp_output);
    33     rewind(fp_input);
    34 
    35     int ch;
    36     while((ch = fgetc(fp_output)) != EOF)
    37     {
    38         fputc(ch, fp_input);
    39     }
    40     
    41     rewind(fp_output);
    42     rewind(fp_input);
    43 
    44     int ch_output, ch_input;
    45 #if 1
    46     printf("Outputfile %s <file> : ", argv[1]);
    47     while((ch_output = fgetc(fp_output)) != EOF)
    48     {
    49         fputc(ch_output, stdout);
    50     }
    51     //#else
    52     printf("Inputfile %s <file> : ", argv[2]);
    53     while((ch_input = fgetc(fp_input)) != EOF)
    54     {
    55         fputc(ch_input, stdout);
    56     }
    57 #endif
    58 
    59 err1 :
    60     fclose(fp_input);
    61 err :
    62     fclose(fp_output);
    63 
    64     return 0;
    65 }
    goto语句经典运用实例

       14.提醒 : 尽量少使用exit(1);

      15.diff : 判断两个文件内容是否相同

      16.  int fseek(FILE *stream, long offset, int whence)

    whence SEEK_SET    文件开头
    SEEK_CUR    文件当前位置
    SEEK_END    文件末尾

      17.rewind(FILE *fp)  返回文件头

      18.ftell : 返回文件读写位置

      19.计算文件长度 :

      1 #include <stdio.h>
      2 #include <errno.h>
      3 #include <string.h>
      4 
      5 int main(int argc, char *argv[])
      6 {
      7     if(argc < 2) {
      8         fprintf(stderr, "The %s : no <file>.
    ", argv[0]);
      9         return 1;
     10     }
     11 
     12     FILE *fp;
     13 
     14     if(NULL == (fp = fopen(argv[1], "r"))) {
     15         fprintf(stderr, "The file %s : %s
    ", argv[1], strerror(errno));
     16         return 1;
     17     }
     18 
     19     fseek(fp, 0, SEEK_END);
     20     int len = ftell(fp);
     21 
     22     printf("%s:	%d
    ", argv[1], len);
     23 
     24     fclose(fp);
     25 
     26     return 0;
     27 }
    文件长度

       20.在文件中 : 初始值不为0,在.data段

              初始值为0, 在.bss段

       今天的英语: positioned(定位)  exist(存在)  inital(初始)  truncate(截短)

                                                今天还不错。就是宝贝有点忙。。

                      

                                                                                                              想你喃!!

  • 相关阅读:
    《原創》實現禁止 WTL CTabView 中標籤的拖曳行為。 (Disable Dragging Operation of CTabView)
    《轉貼》ATL NTService 運作流程
    《轉貼》WTL 之 m_hWndMDIClient
    《原創》加強版的 C++ 字串型別
    《原創》建立最基礎的 Irrlicht 3D 引擎的應用框架。(for vc2008)
    《轉貼》關於 ios::app 與 ios::ate 簡易說明
    《轉貼》WTL for MFC programmer 系列文章
    《轉貼》以Virtual Inheritance及Template讓C++能實現C#中的sealed
    Oracle学习笔记(concat和substr)
    Oralce学习笔记(sql取一张表的数据插入另一张表)
  • 原文地址:https://www.cnblogs.com/cxw825873709/p/3265011.html
Copyright © 2011-2022 走看看