Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Formatting 如何在C中打开和读取文件?_Formatting_Fread - Fatal编程技术网

Formatting 如何在C中打开和读取文件?

Formatting 如何在C中打开和读取文件?,formatting,fread,Formatting,Fread,我正在启动一个新程序,第一步是读取以前程序中编写的文件。程序无法在fseek()行和fread()行之间运行,出现以下错误: debug assertion failed expression:(stream!=NULL) no such file or directory exists 我的代码如下: #include <stdio.h> #include <string.h> int main (void) { int m = 0; char

我正在启动一个新程序,第一步是读取以前程序中编写的文件。程序无法在
fseek()
行和
fread()
行之间运行,出现以下错误:

debug assertion failed
expression:(stream!=NULL)
 no such file or directory exists
我的代码如下:

#include <stdio.h>
#include <string.h>

int main (void)
{
    int m = 0;
    char stream[20];
    FILE *fp;

    /* opens a file which had data written into it in a previous program */
    fp = fopen("codetree.bin", "r");

    /* makes sure the file pointer is set at the beginning of the file */
    fseek(fp, 0, SEEK_SET);

    m = sizeof(stream);
    fread(&stream, m, 1, fp);
    /* ... */
}
我得到以下错误:

debug assertion failed
expression:(stream!=NULL)
 no such file or directory exists

我在代码中忽略了什么或遗漏了什么?

您在这里观察到的问题是什么?它说“调试断言失败”和“expression:(stream!=NULL)”之后,它会中止运行代码。您的程序无法打开
codetree.bin
(可能它找错了位置?)。您应该添加代码以检查
fp!=空
fopen()
之后,然后继续执行其余代码。啊。我添加了那行代码,这确实是问题所在。谢谢你的帮助。因此,当我添加以下行时//if(fp==NULL)printf(“fopen()错误…%s\n”,strerror(errno));//它说“不存在这样的文件或目录”。我的源文件中有codetree.bin,是不是放错地方了?