zoukankan      html  css  js  c++  java
  • C语言IO操作总结

         C语言IO操作总结
    C程序将输入看做字节流,流的来源是文件、输入设备、或者另一程序的输入;
    C程序将输出也看做字节流;流的目的是文件、视频显示等;

    文件处理:
    1 :fopen("filename ","mode") 返回文件指针
    mode可以为"r、w、a、r+、w+、a+"
    r:读文件
    w:写文件 不存在则自动创建
    a:尾部追加文件 不存在则自动创建
    r+ 打开文件、可读可写模式
    w+ 打开文件、可读、可写如果存在则将文件截短为0 不存在则自动创建
    a+ 打开文件、可读整个文件、可写文件尾部 不存在则自动创建
    2: getc(fp)返回文件的字符
    3 :putc(fp,stdout)等同于 putchar()
    putc (fp, fpout) 将fp指向的文件写入到fpout中;
    4 : fclose() 成功关闭文件返回0 否则返回EOF;
    实例代码

    实例效果 :(需要新建一个file.txt文件并输入内容如XXX) 每次执行完毕之后会会将flie.txt文件内容复制到filecopy.txt文件结尾位置; 

     1 #include <stdio.h>
     2 #include <stdlib.h> // ANSI C exit() prototype
     3 int main(int argc, char *argv[])
     4 {
     5 
     6     char ch;         // place to store each character as read
     7     FILE *fp;       // "file pointer"
     8     long count = 0;
     9     FILE *fpp;
    10 //    char *filename;
    11 //    scanf("%s",filename);
    12 //    filename="file.txt";
    13 
    14     if ((fp = fopen("file.txt", "r+")) == NULL)
    15           printf("Can't open ");
    16      if ((fpp= fopen("filecopy.txt", "a")) == NULL)
    17           printf("Can't open ");
    18     while ((ch = getc(fp)) != EOF)
    19     {
    20        putc(ch,fpp);  // same as putchar(ch);
    21        count++;
    22     }
    23     fclose(fp);
    24     printf("File  has %ld characters
    ", count);
    25 
    26     return 0;
    27 
    28 }
    View Code

    5 ;fprintf 和 printf一样 只是多了一个文件指针的参数 

    int fprintf(FILE *stream, char *format, <variable-list>); 输出到文件中
    fscanf()类似于fprintf();
    6 :fgets()文件读取字符串函数
    char* fgets(char* ,MAX,FILE*)
    从文件中读取字符串,最多字符数为MAX,如果遇到' '或者缓冲区满则结束,并且在字符串结尾加空字符;
    7:fgetc(fp)从文件中读出一个字符;其中getc()与fget函数作用相同但是getc()不是函数调用是宏定义;
    8:feof(fp)如果到达文件结尾返回非零
    9:fseek(fp,offset,position) position=SEEK_SET/SEEK_CUR/SEEK_END 从文件头开始/从当前读到的位置开始/从文件结尾开始 后两个可以为负值
    10:rewind(fp); 返回文件开头位置;
    11:fllush(fp);更新缓冲区;
    12:ftell(fp) 返回当前读取到的位置;
    实例代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
      FILE * f;
      char ch;
      char *gets_;
    //  char *gets_s;
    //  int s;
    //  char str[]="aaa";
    //printf("asdfasdfsadf");
      if((f=fopen("pra.txt","a+"))!=NULL)
           while((ch=fgetc(f))!=EOF)
             printf("%c",ch);
        fclose(f);
    
        if((f=fopen("pra.txt","a+"))!=NULL)
           if((ch=fgetc(f))!=EOF)
             {
                printf("%c",ch);
              if((ch=fgetc(f))!=EOF)
                 printf("%c",ch);
              }
        fclose(f);
       printf("
    ");
       if((f=fopen("pra.txt","a+"))!=NULL)
       {
        printf("fgets函数 输出>>>>%s",fgets(gets_,4,f));
        printf(">>>>%s
    ",fgets(gets_,4,f));
        fclose(f);
       }
    
        if((f=fopen("pra.txt","a+"))!=NULL)
       {
        printf("fgetc函数输出<<<%c",fgetc(f));
        //printf中 后一个fgetc()先执行?
        printf("<<<%c%c
    ",fgetc(f),fgetc(f));
        fclose(f);
       }
    
      if((f=fopen("pra.txt","a+"))!=NULL)
       {
          while(!feof(f))
          {
            printf("%c",fgetc(f));
            printf("读到的位置为:%d
    ",ftell(f));
          }
          fclose(f);
       }
    //     s=fprintf(f,"hi
    ");
    //  gets_s=gets(gets_);
    //  puts(gets_);
    //  printf("%d",strlen(str))
    
    
     return 0 ;
    }
    View Code

    13:设置文件缓冲区 setvbuf(fp,char*buf,mode,size);

    buf为自定缓冲区;size为缓冲区大小
    mode:_IONBF _IOLBF _IOFBF 无缓冲IO/以换行为依据的无缓冲IO/完全无缓冲IO
    14:fread(void*buffer,size,count,fp) 从文件中读内容到缓冲区buffer
    buffer 接受数据的内存地址
    size:单个元素的大小(单位是字节)
    count:需要读元素个数;
    15:fwrite同fread 只是前者是写入文件中,后者是从文件读出;

    fread fwrite函数比较常用与把某种数据结构以文件形式保存在文件中

    参考自:http://see.xidian.edu.cn/cpp/u/hs6/

    http://my.oschina.net/myriads/blog/2849

    阿南 On the way.
  • 相关阅读:
    RN-Android构建失败:Caused by: org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'AwesomeProject'.
    Android更新包下载成功后不出现安装界面
    真机调试: The application could not be installed: INSTALL_FAILED_TEST_ONLY
    react native 屏幕尺寸转换
    Android Studio生成签名文件,自动签名,以及获取SHA1和MD5值
    React Native安卓真机调试
    git提交代码报错Permission denied, please try again
    The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.
    命令行设置快捷命令
    Linux 常用指令
  • 原文地址:https://www.cnblogs.com/RealMan/p/3632337.html
Copyright © 2011-2022 走看看