c - 恐惧的问题

标签 c fread

我试图一次从一个文件中读取一个字节:

size_t result_new = 1;  
char buf6[1];  
if( (result_new = fread(buf6, 1, 1, pFile)) != 1)  
            {  
                printf("result_new = %d\n", result_new);
                printf("Error reading file\n");
                exit(1);
            }

result_new 变为 0 并打印错误。任何想法可能是错的。我确定 pFile 没问题。

谢谢

最佳答案

根据文档:

fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

那么,您为什么不检查能够回答您的问题的错误代码呢?您可以使用 perror ,例如。

关于c - 恐惧的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4830694/

相关文章:

c - 关于从链表中删除节点时释放节点

c - C 中字符串的差异

c - "+&r"与 "+r"有何不同?

python - Matlab -> Python。从磁盘读取异构一维二进制数组

c - 如何使用 fread 和 fwrite 从文件中读取 pcm 样本?

c - 为什么 c 中的 fread() 读取额外的 '#newlines' 个字符?

c - 有没有一种方法可以将一个文本文件中的每一行与 C 中另一个文本文件中的一行进行比较?

c++ - BFS 算法 - 具有约束步骤的网格上的最短步行

r - 使用fread导入csv时出现“字符串中嵌入nul”错误

c++ - C/C++ 读取具有多个字符串和字符的二进制文件