【发布时间】:2013-11-22 03:59:23
【问题描述】:
我正在尝试解析文件每一行中的每个标记。但是我在内部while循环中得到了一个无限循环。为什么是这样?这不是因为“真”条件,因为我在 while 循环中有 break 语句。我认为我不正确 检查我何时到达行尾。
char currentLine[MAXIMUM_LINE_LENGTH + 1]; // + 1 for terminating char
while (true) {
if (fgets(currentLine, MAXIMUM_LINE_LENGTH, inputFilePointer) == EOF ) {
break;
} else { // we need to parse the currentLine
// check if the currentLine is too long
if (strlen(currentLine) > MAXIMUM_LINE_LENGTH) {
// print to the file that the currentLine being parsed it too long
printf(
"WARNING: currentLine is too long and must be <= 256 characters\n");
continue; // move on to next line in input file
} else { // currentLine has valid length
while (true) {
// iterate through each token within the currentLine and parse the token
char *currentToken = strtok(currentLine, " $,\t");
printf("currentToken = %s", currentToken);
// do something with currentToken
if (currentToken == NULL || *currentToken == '#') {
break; // <== ERROR This break statement is never getting hit WHY?
}
}
}
}
}
【问题讨论】: