printf("Batch Mode\n");
FILE* batchFile;
char oneLine[LINE_MAX];
batchFile = fopen(argv[1], "r");
bool done = false;
if(batchFile == NULL)
{
perror("File");
exit(1);
}
while (fgets(oneLine,LIMIT,batchFile) != NULL && !done)
{
processLine(oneLine, done);
}
所以我对上面的代码有些担心。问题是,即使行只包含换行符,fgets仍会获取行。所以我需要消除这个,或者至少能够检查只包含换行符的行。
我试过
if (strcpy(line, '\n') == 0)
{
printf("an Enter key line\n");
return;
}
但它仍然没有用。
答案 0 :(得分:0)
如何检查fgets何时返回空行?
当一行包含“\ n”时,它不被视为空。
fgets()
的返回包含函数的返回结果。如果成功,该函数返回一个指向NUL终止字符串的指针。如果函数遇到文件结尾并且没有在lineBuffer中读取任何字符,则返回NULL指针。如果发生读取错误,则fgets返回NULL并将errno设置为非零值。
所以,检查所有可能性。
更改此行:
while(fgets(oneLine,LIMIT,batchFile) != NULL && !done) //LIMIT is not the size of
//the buffer oneLine
到
while (fgets(oneLine,LINE_MAX,batchFile) != NULL && !done)
{
if (errno != 0 ) {//handle error, exit}
//proceed with normal line processing
if (strcmp(oneLine, "\n") != 0)//strcmp verifies line has more than only \n
{
processLine(oneLine,done);
}