fread()的问题总是返回1 [英] problems with fread() always returning 1

查看:498
本文介绍了fread()的问题总是返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过类似的问题,但是我的问题有所不同。我确保以二进制模式打开文件,并检查读取时是否发生错误。

I have looked at similar questions, but mine is a bit different. I make sure to open file in binary mode and to check if error occurs while reading.

文件内容:

message1, message2, 53467

程序可读取简单文件:

program to read simple file:

int bytesRead;
FILE* CSV;
CSV = fopen("\\Temp\\csv.txt", "rb");
char dataBuf[128];

while ( (bytesRead = fread(dataBuf, 1, sizeof(dataBuf), CSV) > 0) )
{
        if (ferror(CSV))
            //handle error

        //do stuff with dataBuf contents
}

fread()始终返回1。也不输入错误,因此不会出现文件读取错误。但是,char数组dataBuf充满了文件中的整个消息。我将fread与另一个函数结合使用,因此我需要知道使用fread()读取了多少字节。有什么想法吗?

fread() is always returning 1. ferror is also not entered, so no file reading error. However, the char array dataBuf is filled with the entire message from the file. I am using fread in conjunction with another function so I need to know how many bytes were read using fread(). Any ideas?

推荐答案

优先级很重要。

在作业周围添加括号。

Add parenthesis around assignment.

while  (  (  bytesRead = fread(dataBuf, 1, sizeof(dataBuf), CSV)  )  > 0  )   
          ^                                                       ^

请参见 C_Operator_Precedence_Table

如果您看到 1.5.1文件复制 C编程语言中的部分> Brian W. Kernighan和Dennis M. Ritchie 您将对此有清楚的解释。

If you see 1.5.1 File Copying section of The C programming Language By Brian W. Kernighan and Dennis M. Ritchie You will get clear explanation on This.

这篇关于fread()的问题总是返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆