调试断言失败,VSTS2010中显示表达式(stream!= NULL)

时间:2013-11-14 03:11:26

标签: c visual-studio-2010

您好我有以下代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    char c;
    printf("Contents of the file before appending:\n");
    fp=fopen("E:\Append.txt","r");

    while(!feof(fp))
    {
        c=fgetc(fp);
        printf("%c",c);
    }

    fp=fopen("E:\Append.txt","a");

    if(fp==NULL)
    {
        printf("The File cannot be appended");
        exit(1);
    }
    printf("Enter String to Append:");

    fp=fopen("E:\Append.txt","w");

    while(c!='.')
    {
        c=getche();
        fputc(c,fp);
    }
    fclose(fp);

    printf("Contents  of the file after Appending");

    fp=fopen("E:\Append.txt","r");

    while(!feof(fp))
    {
        c=fgetc(fp);
        printf("%c",c);
    }

}

但是当我尝试在VSTS2010中运行代码时,我收到以下消息

“Debug Assertion Failed!Program:E:\ Programs \ VSTS \ 14.1 \ Debug \ 14.1exe File:f:\ dd \ vctool \ crt_bld \ self_X86 \ crt \ src \ feoferr.c Line:44

Expression(Stream!= NULL)“

请帮我解决问题。在此先感谢。

1 个答案:

答案 0 :(得分:0)

您没有查看任何fopen来电的返回值。然后,您调用对文件流起作用的函数,并断言stream != NULL失败。

您需要检查返回值。

fp = fopen(some_path);
if (!fp) {
    // error
}

看起来像是对feof的一次调用。