【发布时间】:2018-02-24 20:16:18
【问题描述】:
由于某种原因,fopens 在我的程序中不断崩溃。
当我读取输入文件并将内容放入变量时它会工作一次。但是由于某种原因,当我尝试让它再次使用 fopens 时,它崩溃了......
有人可以帮忙
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *buf = "\0";
char buffer[99999] ;
int main(int argc, char *argv[])
{
ptr_file =fopen(inputFileLocation,"r");
if (!ptr_file)
{
return 1;
}
while (fgets(buf,maxvalue, ptr_file)!=NULL)
{
strcat(buffer,buf);
}
fclose(ptr_file);
... //workings
//then the program crashes if I add an fopen() and fclose() at the end
}
【问题讨论】:
-
发布一个显示您的问题并且可编译的最小示例。如果您无法展示到目前为止的问题,那么没有人会提供帮助。
-
@joe 没什么好展示的,当包含 fopen() 和 fclose() 时,程序只是崩溃,没有错误消息
-
显示代码。很多人多次使用 fopen 和 fclose 都没有问题,所以问题很可能在您的使用中。
-
你要发布不起作用的代码吗?
-
您正在将
fgets用于单个字符缓冲区(可能是只读的,但无论如何您几乎肯定会覆盖其他内存)。为什么不直接使用fgets到buffer?