我有一个可以完美运行的 main
函数。它将指向 FILE
的指针传递给我的 readFile
函数,它应该输出文件的数据并删除空行。发生的情况是它输出每一行包括空行。我彻底检查了我的代码,似乎找不到问题。任何帮助将不胜感激!
int
check_whitespace (char *line)
{
while (*line)
{
if (!('\n' == *line || '\t' == *line || ' ' == *line))
return 0;
line += 1;
}
return 1; /* returns 1 if line is ALL blanks */
}
int
readFile (FILE * fp)
{
char arrbuff[BUFFSIZE];
while (fgets (arrbuff, BUFFSIZE, fp) != NULL)
if (!check_whitespace (arrbuff))
{ /* 0 = goodline */
fputs (arrbuff, stdout);
}
fclose (fp);
}
最佳答案
尝试更改此行
if (!('\n' == *line || '\t' == *line || ' ' == *line))
return 0;
在您的代码中
if (!isspace(*line))
return 0;
您需要添加
#include <ctype.h>
我猜您正在 Windows 下运行,并且每行都以 \r\n
结尾,并且您没有选择 \r
。
来自手册页:
isspace()
checks for white-space characters. In the "C" and "POSIX" locales, these are: space, form-feed ('\f'
), newline ('\n'
), carriage return ('\r'
), horizontal tab ('\t'
), and vertical tab ('\v'
).
即使这不能解决问题,它也会使您的代码更具可读性。
或者,进行最小的更改:
if (!('\n' == *line || '\t' == *line || ' ' == *line || '\r' == *line))
return 0;
关于c - 关于我的C程序跳过空行的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038261/