I'm having a hard time with a do-while loop, that is supposed to stop
when we reach the end of the file. Here's the loop code:
do {
if (pcompanyRow[0] != '#' && pass == 1) {
strtok(pcompanyRow, ":");
pcompanyName = strcpy(pcompanyName, strtok(NULL, ""));
pass = 2;
fgets(pcompanyRow, 1024, f);
}
if (pcompanyRow[0] != '#' && pass == 2) {
strtok(pcompanyRow, ":");
pcompanySMSPrice = strcpy(pcompanySMSPrice, strtok(NULL , ""));
pass = 3;
fgets(pcompanyRow, 1024 , f);
}
if (pcompanyRow[0] != '#' && pass == 3) {
strtok(pcompanyRow, ":");
pcompanyMMSPrice = strcpy(pcompanyMMSPrice, strtok(NULL, ""));
pass = 4;
fgets(pcompanyRow, 1024, f);
}
if (pass == 4) {
AppendCompanyNode(pcompanyList, pcompanyName, pcompanySMSPrice, pcompanyMMSPrice);
pass = 1;
}
} while (!feof(f));
After running with the debugger, I noticed that all the crash problems I have are because it doesn't go out of this loop even when it reached the whole lines.
How should I write it correctly?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…