【发布时间】:2014-08-14 22:23:56
【问题描述】:
int main()
{
FILE *file1, *file2;
char filename[] = "test.xml";
char c;
int line = 1;
//open file in read mode
file1 = fopen(filename, "r");
c = getc(file1);
while (c != EOF){
printf("%c", c);
c = getc(file1);
}
//rewind
rewind(file1);
//fseek(file1, 0, SEEK_SET);
//open new file in write mode
file2 = fopen("replica.c", "w");
c = getc(file1);
if(c == EOF) printf("toto");
}
rewind() 和 fseek() 函数不起作用,我的程序显示“toto”,所以 file1 仍然定位在 EOF 上。
请问您有解决这个问题的想法吗?
【问题讨论】:
-
printf("%c", c);打印什么?如果您的程序打印的所有内容都是toto,那么这可以通过文件test.xml为空来解释。 -
您应该将
c声明为int类型,而不是char。getc()函数返回一个int,因此可以将字节值 255 = 0xFF 与 EOF = -1 区分开来。 -
文件是空的...抱歉,谢谢 pts。