fread函数读.dat文件,中途退出原因分析。

fread函数读,但有时候不能读完,后来网上查了下,见下文:
最近写一个程序,发现用fread读“.dat”文件时不能读完整个文件,后来用hex格式观察读文件退出位置的数字,才发现只要是读到0x1A时,fread就认为结束!后来看了MSDN后知道用text模式打开文件时,系统默认CTRL+Z为文件结束符,而0x1A刚好就是CTRL+Z的ASCII码。另外在另一个文章里面说到如果遇到“/r/n”,也将被映射为“/n”。因此如果是读普通数据而非文本,以以下格式打开文件。datafile = fopen(“whatever.dat”, “rb”); 这样就可以解决以上问题!在MSDN中,对于"t"和"b"打开模式的原文如下:
t Open 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, may cause fseek to behave improperly near the end of the file.

b是二进制模式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值