关于getline函数的Sigsegv错误

时间:2017-08-02 18:25:55

标签: c++

我花了最近几个小时试图调试这段代码,我不能在我的生活中追查这个错误的来源。

void readSaveFile() {
int cellIdx = 0;
ifstream myFile;
string nextWord;
string nextLine;
char* tok;
const char* delim = " ,;/\n\t";
char thisLine[30];
myFile.open("VoronoiSave1.txt");
//int idx = 0;
if (!myFile.is_open()) {
    cout << "Unable to open file" << endl;
} else {
    while (!myFile.eof()) {
        getline(myFile, nextLine);
        strcpy(thisLine, nextLine.c_str());
        tok = strtok(thisLine, delim);
        if (tok != 0x0) {
            nextWord = tok;
        } else break;
            if (nextWord == "xBound") {
                tok = strtok(NULL, delim);
                xBound = atof(tok);
                midXBound = xBound /4;
            } else if (nextWord == "yBound") {
                tok = strtok(NULL, delim);
                yBound = atof(tok);
                midYBound = yBound /4;
            } else if(nextWord == "sizeFactor") {
                tok = strtok(NULL, delim);
                sizeFactor = atoi(tok);
            } else if (nextWord == "numberCells") {
                tok = strtok(NULL, delim);
                numberCells = atoi(tok);
            } else if (nextWord == "numberGenerations") {
                tok = strtok(NULL, delim);
                numberGenerations = atoi(tok);
            } else if (nextWord == "displayEvery") {
                tok = strtok(NULL, delim);
                displayEvery = atoi(tok);
            } else if (nextWord == "A0") {
                tok = strtok(NULL, delim);
                A0 = atof(tok);
            } else if (nextWord == "P0") {
                tok = strtok(NULL, delim);
                P0 = atof(tok);
            } else if (nextWord == "conA") {
                tok = strtok(NULL, delim);
                conA = atof(tok);
            } else if (nextWord == "conB") {
                tok = strtok(NULL, delim);
                conB = atof(tok);
            } else if (nextWord == "conC") {
                tok = strtok(NULL, delim);
                conC = atof(tok);
            } else if (nextWord == "conE") {
                tok = strtok(NULL, delim);
                conE = atof(tok);
            } else if (nextWord == "c1") {
                tok = strtok(NULL, delim);
                c1 = atoi(tok);
            } else if (nextWord == "c2") {
                tok = strtok(NULL, delim);
                c2 = atoi(tok);
            } else if (nextWord == "c3") {
                tok = strtok(NULL, delim);
                c3 = atoi(tok);
            } else if (nextWord == "c4") {
                tok = strtok(NULL, delim);
                c4 = atoi(tok);
            } else if (nextWord == "end") {
                tok = NULL;
                break;
            } else if (nextWord == "|"){
                point p;
                tok = strtok(NULL, delim);
                p.x = atof(tok);
                tok = strtok(NULL, delim);
                p.y = atof(tok);
                points.push(p);
                listOfCells[cellIdx].setLocation(p);
                cellIdx++;
            }  
            else tok = NULL;
    //  }
    }
}

myFile.close();
}

该函数应该逐行读取txt文件,并将其中的值分配给我的代码的变量。调试器告诉我它在读取文件的中间某处getline()函数失败(每次都是文本文件的同一行,我检查了该行中的任何异常)。我有另一个类似于这个的功能,不同之处在于它读取较小的文件,并且它工作正常(它们是相同的,除了这个也读取坐标集)。

实际上,当我现在尝试调试它以获取特定错误时,它只会给我一个sigtrap错误(ntdll!RtlpntMakeTemporaryKey())

这是它的文本文件:

xBound 20
yBound 20
sizeFactor 25
numberCells 64
numberGenerations 180000
displayEvery 10
A0 6.25
P0 15
conA 1
conB 0.4
conC -2
conE 0.5
c1 52
c2 9
c3 59
c4 16
| 0.137451,7.39333
| 5.42329,4.04438
| 10.4966,2.11276
| 14.8588,8.83051
| 8.51037,9.98535
| 14.5657,0.96855
| 12.2967,8.02728
| 12.67,8.43055
| 6.71378,9.69364
| 9.91587,9.71739
| 0.0102949,14.9428
| 7.64323,8.52529
| 8.56282,11.6512
| 9.4758,4.41921
| 8.91906,6.94152
| 6.6964,6.83924
| 14.3277,10.358
| 8.52992,11.7173
| 10.0237,11.6031
| 6.95512,5.82018
| 8.91503,9.84806
| 11.5676,8.50173
| 4.9829,7.02023
| 13.3241,7.1157
| 13.8758,6.79126
| 11.4524,4.6928
| 13.792,10.9609
| 19.8873,10.8045
| 8.87628,15.5246
| 10.643,6.70885
| 5.95795,8.35663 <- this is the line that it fails at
| 14.3533,10.3611
| 6.94571,8.4644
| 7.55378,5.5384
| 9.64479,5.73394
| 13.4751,9.00817
| 8.19452,5.03502
| 6.15974,6.47267
| 11.1405,9.72743
| 5.77898,10.5858
| 13.3271,7.15259
| 11.6175,1.17234
| 10.1269,8.62241
| 13.5455,9.98899
| 10.0209,1.27546
| 10.9164,8.19685
| 9.07094,6.50686
| 10.9881,5.52526
| 10.3319,7.48381
| 19.9812,14.545
| 4.91259,8.48509
| 9.856,6.025
| 11.7527,5.86879
| 19.7908,8.19586
| 11.8846,9.9203
| 8.53876,11.6635
| 9.39078,11.2209
| 0.0414163,14.957
| 10.9526,7.18578
| 5.5856,8.55156
| 1.39154,6.49213
| 8.45031,11.5184
| 19.9807,13.2524
| 6.12546,9.69112

谢谢!

编辑:标记txt文件中的问题行

0 个答案:

没有答案