while ((ch = getc(in)) != EOF) � 编译程序不断显示乱码
直接复制书上的代码执行得到的却是不停的乱码,完全不是书上说的输出内容。这是书上原文复制:
Program P6.11
//This program prints the data from a file numbering the lines
#include <stdio.h>
int main() {
char ch;
FILE *in = fopen("input.txt", "r");
int lineNo = 1;
printf("%2d. ", lineNo);
while ((ch = getc(in)) != EOF) {
putchar(ch);
if (ch == '\n') {
lineNo++;
printf("%2d. ", lineNo);
}
}
fclose(in);
}
Assume the input file contains the following:
There was a little girl
Who had a little curl
Right in the middle of her forehead
Program P6.11 will print this:
1. There was a little girl
2. Who had a little curl
3. Right in the middle of her forehead
4.
[此贴子已经被作者于2019-10-3 23:39编辑过]