是否可以连续测试两个换行符?
Is it possible to test for two new line characters in a row?
我正在尝试验证文件中包含实际内容的行,并在连续有两个空行的情况下退出。这可以做到吗?
此代码导致 fgetc() 未捕获双回车 returns/new 行。
代码是来自 C89 项目的片段,因此在片段上方进行声明。
if ((file = fopen(fileName,"r")) == NULL)
{
free(fileName);
exit(1);
}
while (c != EOF)
{
cOld = c;
c = fgetc(file);
/* count lines */
if(c == '\n'){
newLine++;
}
/* test for two carriage returns in a row */
if(c == '\n' && cOld == '\n'){
printf("ERROR: Invalid File\n");
free(fileName);
exit(1);
}
}
看来你是 运行 你在 windows 上的节目。在 windows 中,行尾由“\r\n”表示。
因此当您检查 c 和 cOld 时,它们不会同时持有 '\n'。
Text files created on DOS/Windows machines have different line endings
than files created on Unix/Linux. DOS uses carriage return and line
feed ("\r\n") as a line ending, which Unix uses just line feed ("\n").
You need to be careful about transferring files between Windows
machines and Unix machines to make sure the line endings are
translated properly.
请按照此 link 了解更多详情:
http://www.cs.toronto.edu/~krueger/csc209h/tut/line-endings.html
我正在尝试验证文件中包含实际内容的行,并在连续有两个空行的情况下退出。这可以做到吗? 此代码导致 fgetc() 未捕获双回车 returns/new 行。
代码是来自 C89 项目的片段,因此在片段上方进行声明。
if ((file = fopen(fileName,"r")) == NULL)
{
free(fileName);
exit(1);
}
while (c != EOF)
{
cOld = c;
c = fgetc(file);
/* count lines */
if(c == '\n'){
newLine++;
}
/* test for two carriage returns in a row */
if(c == '\n' && cOld == '\n'){
printf("ERROR: Invalid File\n");
free(fileName);
exit(1);
}
}
看来你是 运行 你在 windows 上的节目。在 windows 中,行尾由“\r\n”表示。
因此当您检查 c 和 cOld 时,它们不会同时持有 '\n'。
Text files created on DOS/Windows machines have different line endings than files created on Unix/Linux. DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n"). You need to be careful about transferring files between Windows machines and Unix machines to make sure the line endings are translated properly.
请按照此 link 了解更多详情: http://www.cs.toronto.edu/~krueger/csc209h/tut/line-endings.html