C语言读写文件 -fopen、fread、fwrite

在C语言中,使用fopen、fread、fwrite进行文件操作时,若以文本模式(t)打开文件,可能会遇到CTRL+Z作为EOF、换行符转换等问题,导致读写不完整。为避免这种情况,可以使用二进制模式(b)。不指定模式时,默认可能为文本模式,此时fread遇到控制字符会停止读取。使用二进制模式则能避免这类问题,并确保正确读取文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fopen()函数中第一个形式参数表示文件名, 可以包含路径和文件名两部分。
如:
"B:TEST.DAT"
"C:\\TC\\TEST.DAT"
如果将路径写成"C:\TC\TEST.DAT"是不正确的, 这一点要特别注意。
第二个形式参数表示打开文件的类型。关于文件类型的规定参见下表。
表 文件操作类型
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
字符 含义
────────────────────────────
"r" 打开文字文件只读
"w" 创建文字文件只写
"a" 增补, 如果文件不存在则创建一个
"r+" 打开一个文字文件读/写
"w+" 创建一个文字文件读/写
"a+" 打开或创建一个文件增补
"b" 二进制文件(可以和上面每一项合用)
"t" 文这文件(默认项)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
如果要打开一个CCDOS子目录中, 文件名为CLIB的二进制文件, 可写成:
fopen("c:\\ccdos\\clib", "rb");
如果成功的打开一个文件, fopen()函数返回文件指针, 否则返回空指针
(NULL)。由此可判断文件打开是否成功。


int fread(void *buf, int size, int count, FILE *stream);
int fwrite(void *buf, int size, int count, FILE *stream);
fread()函数是从文件中读count个字段, 每个字段长度为size个字节, 并把
它们存放到buf指针所指的缓冲器中。
如果调用成功返回实际读取到的元素个数(个数以size为单位),如果不成功返回 0。

fwrite()函数是把buf指针所指的缓冲器中, 长度为size个字节的count个字
段写到stream指向的文件中去。返回实际写入的数据项个数count。

若无fclose()//未练习
函数, 则不会向文件中存入所写的内容或写入的文件内容不全。有一个对缓冲区
进行刷新的函数, 即fflush(),

fread,对指定长度的文件数据。读取的长度远小于文件的总长度:

查MSDN,fopen最后一个参数:

tOpen in text (translated) mode.

In this mode, CTRL+Z is interpreted as an end-of-file character on input.

In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible.

This is done because using fseek and ftell to move within a file that ends with a CTRL+Z can cause fseek to behave improperly near the end of the file.

Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output.

When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters.

For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters.

bOpen in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

If t or b is not given in mode, the default translation mode is defined by the global variable _fmode.

If t or b is prefixed to the argument, the function fails and returns NULL.  

默认应该是文本模式t,这个时候fread遇到控制字符就不往后读了

使用binnay mode 这样就能就没有上述问题了,一个简单的例子:

复制代码
  fp=fopen("number.txt","wb");//wb 写入二进制文件
  count=fwrite(src,sizeof(int),n,fp);
  printf("\n%d",count);
  fclose(fp);
  fp=fopen("number.txt","rb");//读取二进制文件 文本格式读出错
  if(fp==NULL)
          printf("error");
  int *num=(int *)calloc(n,sizeof(int));
  count=fread(num+1,sizeof(int),n,fp);

  ```````````````````````````````````````````````````````````````````````````````````````
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值