C++中文件操作函数详解

文件的打开:FILE *fopen(const char *filename,const char *mode);

文件的关闭:int fclose(FILE *stream);// stream为文件的名称

读数据块函数fread

size_t fread(void *buffer, //数据存放的位置 (数组或指针)

          size_t size, //每一项的字节数

          size_t count,//有多少项

         FILE *stream);//文件指针

返回值:返回count,若出错或文件尾,则小于count

写数据块函数fwrite

size_t fwrite()返回实际所写的count

int feof(FILE *stream); //0表示未到文件结尾,非0表示文件结尾

文件的随机存取函数fseek ,成功则返回0,若出错,返回非0

int fseek(FILE *stream, //文件指针

     long offset, //偏移量(字节数)

     int origin //初始位置(SEEK_CUR:当前位置,SEEK_END:文件尾,SEEK_SET:文件尾)

);

long ftell(FILE *stream)://当前文件指针的偏移字节数,即得到流式文件中的当前位置

eg

i=ftell(fp);

 if(i==-1L) 

   printf(“error\n”);

struct student_type{

  char name[10]; 

  int num;

  int age; 

  char addr[30]

}stu[40];

for(i=0;i<40;i++)

  fread(&stud[i],sizeof(struct student_type),1,fp);

for(i=0;i<40;i++)

  fwrite(&stud[i],sizeof(struct student_type),1,fp);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值