【问题标题】:rewind and fseek don't work - C [closed]倒带和 fseek 不起作用 - C [关闭]
【发布时间】: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 类型,而不是chargetc() 函数返回一个int,因此可以将字节值 255 = 0xFF 与 EOF = -1 区分开来。
  • 文件是空的...抱歉,谢谢 pts。

标签: c eof fseek


【解决方案1】:

如果您的程序打印的所有内容都是toto,那么这可以通过文件test.xml 为空来解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2014-04-12
    相关资源
    最近更新 更多