我正在尝试使用type file.txt | myprogram.exe
通过管道将文件传送到我的程序。我是这样读的:
char *line;
struct block *newblock, *cur;
int i, osl, cbs, rs;
int startblock;
/* read in the blocks ... */
while (!feof(stdin) && !ferror(stdin)) {
cbs = 2048;
line = (char *)malloc(cbs + 1);
if (!line) {
perror("malloc");
exit(1);
}
line[cbs] = '\0';
/* read in the line */
if (!fgets(line, cbs, stdin)) {
free(line);
continue;
} else {
while (line[cbs - 2]) {
/* expand the line */
line = (char *)realloc(line, (cbs * 2) + 1);
if (!line) {
perror("realloc");
exit(1);
}
line[cbs * 2] = '\0';
/* read more */
if (!fgets(line + cbs - 1, cbs + 1, stdin))
break;
cbs *= 2;
}
}
...
}
但就在读取第一行之后,即使文件中有多行,feof()
也会返回true。这怎么会发生呢?
https://stackoverflow.com/questions/56940826
复制相似问题