【发布时间】:2015-09-01 14:32:07
【问题描述】:
我想知道如何在我的 C 代码上解决 Core dumped issue。
当我用:g++ -g MatSim.cpp -o MatSim -lstdc++ -O3 编译它时,我收到三个警告,这是一个(其他两个类似,仅通过字符串变量名区分):
MatSim.cpp: In function ‘int main()’:
MatSim.cpp:200037:27: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
fscanf(TM,"%255s",string2);
我的代码的主要方面以及编译器报告的相关部分:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int to_int(char string[256])
{
if( strcmp(string,"0") == 0 )
{
return 0;
}
...
else if( strcmp(string,"50000") == 0 )
{
return 50000;
}
return -1;
}
int main()
{
int a,b,div,value,k,i,j,tm,ler;
char string[256];
char string1[256];
char string2[256];
FILE *TM;
TM = fopen("TM","r");
if( (TM = fopen("TM","r")) == NULL)
{
printf("Can't open %s\n","TM");
exit(1);
}
fscanf(TM,"%255s",string2);
tm = to_int(string2);
fclose(TM);
...
}
我已经尝试了here 中报告的建议,并试图了解here 中发布的内容。但是,我没有在我的代码中看到它的应用程序。
最后,当我运行exe文件时,它返回:
分段错误(核心转储)`。
【问题讨论】:
-
TM = fopen("TM","r"); if( (TM = fopen("TM","r"))为什么要打开文件两次? -
我的难处是:首先打开文件,然后检查文件是否正常
-
MatSim.cpp:200037:27- 哇。 谢谢没有发布其他 200000 行代码。 -
@beginner 但这不是你的代码所做的。
-
@beginner 另外,如果你使用 C++,你应该注意
streams和std::string的用法。所以选择一种语言——是 C 还是 C++?
标签: c segmentation-fault fopen