gpt4 book ai didi

c - Debug Assertion Failed and Expression(stream!=NULL) 显示在 VSTS2010

转载 作者:太空宇宙 更新时间:2023-11-04 04:46:55 27 4
gpt4 key购买 nike

您好,我有以下代码

#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 中运行代码时,我收到以下消息

“调试断言失败!程序:E:\Programs\VSTS\14.1\Debug\14.1exe 文件:f:\dd\vctool\crt_bld\self_X86\crt\src\feoferr.c 行:44

表达式(流!=NULL)"

请帮我看看哪里出了问题。提前致谢。

最佳答案

您没有检查任何 fopen 调用的返回值。然后调用作用于文件流的函数,它们断言 stream != NULL,但失败了。

您需要检查返回值。

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

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

关于c - Debug Assertion Failed and Expression(stream!=NULL) 显示在 VSTS2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com