我正在做一些作业,想知道是否存在嵌套太多while循环的情况。 嵌套多个while循环是否有缺点? 如果是这样,如何重构下面的代码片段?
以下是逐行读取文件、解析由某些定义分隔符分隔的字段并在打印到控制台之前删除前导空格的代码。
// Read the file one line at a time
while (fgets(lineStr, MAXLINELENGTH, fp) != NULL)
{
charPtr = strtok(lineStr, DELIMITERS);
// Loop until line is parsed
while (charPtr != NULL)
{
// Skip past leading whitespace
while (isspace(*charPtr))
charPtr++;
puts(charPtr);
charPtr = strtok(NULL, DELIMITERS);
}
}
skipSpaces()
或类似的东西。 - Clockwork-Museisspace
,以避免未定义的行为:isspace((unsigned char)*charPtr)
。 - pmg