fread函数读取文件提前结束

最近写了一个读unicode文本的函数,是用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.

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 (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

b

Open 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.

原文地址

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C语言中,可以使用fread函数来读取二进制.dat文件。fread函数的原型为: ``` size_t fread(void *ptr, size_t size, size_t count, FILE *stream); ``` 其中,ptr是指向存储读取内容的数组起始地址的指针;size是要读取的每个数据项的字节大小;count是要读取的数据项的个数;stream是指向待读取文件的指针。 首先,需要创建一个文件指针来指向要读取的.dat文件。例如: ``` FILE *fp; fp = fopen("example.dat", "rb"); // 以二进制只读("rb")方式打开.dat文件 ``` 接下来,可以定义一个数组来存储读取的数据。例如: ``` int data[100]; // 假设要读取100个int类型的数据项 ``` 然后,使用fread函数来读取数据。例如: ``` size_t numItemsRead = fread(data, sizeof(int), 100, fp); ``` 这行代码将会从fp指向的.dat文件中读取100个int类型的数据项,每个数据项的字节大小为sizeof(int),并存储在data数组中。函数的返回值numItemsRead表示实际成功读取的数据项个数。 最后,不要忘记关闭文件。例如: ``` fclose(fp); // 关闭文件 ``` 总结:通过以上步骤,我们可以使用C语言中的fread函数来读取二进制.dat文件中的数据。 ### 回答2: C语言中的fread函数用于从文件中读取数据。具体使用方法如下: 首先,需要打开要读取的.dat文件,可以使用fopen函数进行文件打开操作。例如,以下代码将打开名为"example.dat"的.dat文件,并返回一个指向该文件的指针: ```c FILE *file = fopen("example.dat", "rb"); ``` 其中,第一个参数为要打开的文件名,第二个参数为打开模式。"rb"表示以二进制模式只读方式打开文件。 接下来,可以使用fread函数从打开的文件中读取数据。fread函数的原型如下: ```c size_t fread(void *ptr, size_t size, size_t count, FILE *stream); ``` 其中,第一个参数是指向要读取数据存储位置的指针;第二个参数是每个数据项的大小(以字节为单位);第三个参数是要读取的数据项数量;第四个参数是指向要读取的文件的指针。 例如,以下代码从打开的文件中读取5个整数,并将其存储在一个数组中: ```c int data[5]; fread(data, sizeof(int), 5, file); ``` 在这个例子中,我们将读取5个整数,每个整数的大小为sizeof(int)。读取的数据将被存储在data数组中。 最后,使用fclose函数关闭已经打开的文件。例如: ```c fclose(file); ``` 通过以上步骤,我们可以使用C语言中的fread函数来读取.dat文件中的数据。 ### 回答3: freadC语言中的一个文件读取函数,用于从文件中读取一定数量的数据。 要使用fread读取.dat文件,首先需要打开该文件。可以使用C库函数fopen来打开文件,并指定打开模式为"rb",表示以二进制格式读取文件。比如: ```c FILE* file = fopen("example.dat", "rb"); ``` 然后,可以使用fread函数来读取数据。fread函数的原型为: ```c size_t fread(void *ptr, size_t size, size_t count, FILE *stream); ``` 其中,ptr是用来存储读取数据的内存块的指针;size是每个数据项的字节数;count是要读取的数据项数量;stream是打开的文件指针。 例如,假设.dat文件中存储了一系列整数,可以这样读取: ```c int data[10]; size_t result = fread(data, sizeof(int), 10, file); ``` 上述代码将会从打开的文件中读取10个整数,存储到data数组中。读取的结果会保存在result变量中,结果为实际读取的数据项数量。 最后,读取完成后应该关闭文件,可以使用fclose函数来关闭文件。比如: ```c fclose(file); ``` 以上就是使用fread函数读取.dat文件的基本步骤。需要注意的是,要正确使用fread函数,需要保证读取的数据大小和数量与文件中的数据项相匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值