0

I've spent the last couple hours trying to debug this block of code, and I can't for the life of me track down the source of this error.

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();
}

This function is supposed to read a txt file line by line and assign values from it into variables for my code. The debugger tells me that it fails on the getline() function somewhere in the middle of reading the file (the same line of the text file each time, and I've checked for any irregularities in that line). I have another function that is similar to this one, with the difference being that it reads smaller files, and it works just fine (they're the same except this one also reads sets of coordinates, too).

Actually, when I try to debug it now to get the specific error, it just gives me a sigtrap error (ntdll!RtlpntMakeTemporaryKey())

This is the text file that it reads:

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

Thank you!

Edit: marked the trouble line in the txt file

  • 1
    Did you use a debugger to step through line by line? – Javia1492 Aug 02 '17 at 18:26
  • Yes I did--whenever I step through, everything seems to work fine up until it tries to read the line I marked, then it crashes with either sigsegv or sigtrap on getline() – George Sangiolo Aug 02 '17 at 18:28
  • Well, slightly unrelated but you shouldnt use `.eof()`. https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Javia1492 Aug 02 '17 at 18:30
  • 2
    the obvious thing is that you ran off the end of listOfCells, you dont show its declaration so its hard to guess its size – pm100 Aug 02 '17 at 18:30
  • I checked for that--listOfCells is declared with a size of 4 * numberCells, which is 256--it only feeds numberCells (64) coordinates, so much less than the max – George Sangiolo Aug 02 '17 at 18:33
  • @GeorgeSangiolo Did you actually check the size of `listOfCells` in the debugger? – Javia1492 Aug 02 '17 at 18:34
  • OHH--I just realized I declared listOfCells's size before I read the size in the function – George Sangiolo Aug 02 '17 at 18:37
  • `while (!myFile.eof()) ` is wrong. Google this. Also what is `(tok != 0x0)` ? Why are you even using `tok`? – Ed Heal Aug 02 '17 at 18:39
  • 1
    Instead of `strtok(thisLine, delim);` consider sticking to `std::string` and using `std::string::substr` and `std::string::find_first_of`. It should be a little less explosive. – user4581301 Aug 02 '17 at 18:44

0 Answers0