假设文件hello\n stack overflow \n
中有一个文本文件,输出应该是2
因为有 2 个\n
序列。相反,我得到一个作为答案。我究竟做错了什么?这是我的代码:
int main()
{
FILE *fp = fopen("sample.txt", "r"); /* or use fopen to open a file */
int c; /* Nb. int (not char) for the EOF */
unsigned long newline_count = 1;
/* count the newline characters */
while ( (c=fgetc(fp)) != EOF ) {
if ( c == '\n' )
newline_count++;
putchar(c);
}
printf("\n %lu newline characters\n ", newline_count);
return 0;
}