char string[50], s[50];
File *f = tmpfile();
count = 1;
while (fgets(string, 50, stdin)) {
if (string[0] == '!') {
rewind(f);
} else {
fwrite(string, 50, 1, f);
}
if (strcmp("history\n", string) == 0) {
rewind(f);
while(fgets(s, 50, f)) {
printf("\t%d %s", count, s);
count++;
}
count = 1;
}
}
这段代码的上下文不是很重要。问题是假设fgets
接受“ls”、“date”和“history”。结果输出是:
1 ls
2 3 te
4 5 ory
6
它应该是:
1 ls
2 date
3 history