zoukankan      html  css  js  c++  java
  • C文件读写

    0. 文件操作相关函数

    1.  文件分为字符方式读写,字节方式(二进制方式)读写

      fprintf / fscanf 

    FILE * file;    //全局定义
    
    void save(Book books[]){
        int i;
        if((file = fopen(PATH,FW))==NULL){
            printf("文件保存失败,请检查文件是否存在!");
            return;
        }
        for(i=0;i<cnt;i++){
            fprintf(file,"%05ld %s %s %s %s %s %.2f %.2f
    ",
                books[i].id,books[i].name,books[i].author,books[i].type,books[i].publish,books[i].time,books[i].price,books[i].amount);
        }
        fclose(file);
    }
    void read(Book books[]){
        cnt=0;
        if((file = fopen(PATH,FR))==NULL){
            printf("文件读取失败,请检查文件是否存在!");
            return;
        }
        while(EOF!=fscanf(file,"%ld %s %s %s %s %s %f %f",
            &books[cnt].id,books[cnt].name,books[cnt].author,books[cnt].type,books[cnt].publish,books[cnt].time,&books[cnt].price,&books[cnt].amount)){
                cnt++;
        }
        fclose(file);
    }

      fread / fwrite (二进制方式读写)

      

    2. 判断为空

     FILE * file   NULL!=file

     文件结尾判断  EOF!=scanf 

    3. 清空缓存字符 

    1 fflush(stdin);           /*清除缓存*/
    2 scanf("%d",&MenuItem);   /*获取用户输入字符*/
    3 
    4 fflush(stdin);
    5 x=getchar();

     未完待续...

  • 相关阅读:
    2015第14周四
    2015第14周三
    2015第14周二
    2015第14周一
    2015第13周日
    2015第13周六
    2015第13周五
    2015第13周四
    2015第13周三
    2015第13周二
  • 原文地址:https://www.cnblogs.com/AbcFly/p/6238549.html
Copyright © 2011-2022 走看看