-3
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");
}

The rewind() and fseek() functions don't work, my program display "toto", so file1 is still positioned on EOF.

Do you have an idea to solve this probleme please?

moth
  • 345
  • 1
  • 13
  • 1
    What does the `printf("%c", c);` print? If all your program prints is `toto`, then this can be explained by the file `test.xml` being empty. – pts Aug 14 '14 at 22:25
  • 3
    You should declare `c` as an `int` type, not `char`. The `getc()` function returns an `int` so that a byte value of 255 = 0xFF can be distinguished from EOF = -1. – Colin D Bennett Aug 14 '14 at 22:28
  • The file was empty... Sorry and thank you pts. – moth Aug 14 '14 at 22:45

1 Answers1

2

If all your program prints is toto, then this can be explained by the file test.xml being empty.

pts
  • 80,836
  • 20
  • 110
  • 183