【发布时间】:2017-07-11 12:47:28
【问题描述】:
我编写了一个代码来读取一个文本文件并对其进行一些处理。它在我自己的 PC 和另一个 Linux 系统上正常工作。但是,当我在不同的 Linux 系统上运行它时,“ifstream”命令出现“分段错误(核心转储)”错误。我检查了文本文件,如果它太小,例如两行,cose 工作正常,但是当文件较大时,例如20 行,它因分段错误错误而崩溃。
导致错误的代码段:
int ExtractFragments(int fragmentLength, int overlappingResidues)
{
string line = "", lines = "", interfaceFileName = "";
ifstream interfaceList("tempInterfaceList.txt");
if (interfaceList)
{
bool errFlag = false;
while (getline(interfaceList, interfaceFileName))
{
cout << endl << "interfaces/" << interfaceFileName;
ifstream interfaceFile("interface.txt"); //This line crashes
if (interfaceFile)
cout << "\nHello";
}
}
return 0;
}
任何想法为什么这个 ifsream 会导致分段错误以及如何解决它? 谢谢!
【问题讨论】:
-
你的命令行参数是什么样的?
-
一个minimal reproducible example 会很好...
-
调试器非常适合这个。
gdb <binary name>,run <program options>,backtrace -
@Brandon fragmentLength 为 4,overlapResidues 为 3。
-
@F.Hl 不在任何地方使用。这真的是失败的代码吗?另外什么是其他操作系统?你用什么编译器?