“程序中出现了访问冲突(Segmentation Fault)。”

时间:2011-01-01 18:42:52

标签: c++ dev-c++

我的C ++程序编译并运行,直到我从main()调用此函数:

int uword(){fstream infile("numbers.txt");
fstream exfile("wordlist.txt");
string numb[numoflines];
string lines[numoflines];  
number = 1; 
line = 1;  
for(int i=0;i<numofline;++i)
    {
    getline (infile,number);
    numb[i] = number; //I think this is causing the problem
    getline (exfile,line);
    lines[i] = line; //This too
    }
infile.close();
exfile.close();    
string yourword;

这里的某些东西导致它崩溃,在调试中弹出“程序中出现的访问冲突(分段错误)。”

编辑:我的错误是在for循环中使用!infile.eof。

2 个答案:

答案 0 :(得分:3)

不是直接回答,但我相信这是一个很好的答案......

使用调试器!当分段错误发生时,GDB应该在完全行处暂停,从而为您提供关于错误的非常好的提示。

答案 1 :(得分:1)

getline功能无法按您认为的方式工作。

此外,numoflines中的行数可能超过infile