C file handling

This is a discussion on C file handling within the C Programming forums, part of the General Programming Boards category; Hello friends. I'm a beginner in C programming, and I have a question related to dealing with files. In the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    C file handling

    Hello friends. I'm a beginner in C programming, and I have a question related to dealing with files. In the following piece of code, I'm reading two numbers from a file and saving both, together with their sum, in another file.

    My question is: why is it that the following loop works?

    Code:
      fscanf(arq, "%d%d", &v1, &v2);
      while(!feof(arq))
        {
          sum = v1 + v2;
          FILE *fp = fopen("e79_resultado.txt", "a");
          fprintf(fp, "%d\t%d\t%d\n", v1, v2, soma);
          fclose(fp);
          fscanf(arq, "%d%d", &v1, &v2);
        }
    whereas the loop below does not produce the expected result?

    Code:
      while(!feof(arq))
        {
          fscanf(arq, "%d%d", &v1, &v2);      
          sum = v1 + v2;
          FILE *fp = fopen("e79_resultado.txt", "a");
          fprintf(fp, "%d\t%d\t%d\n", v1, v2, soma);
          fclose(fp);
        }
    Why do I have to read the information once outside the loop to make it work? Aren't they supposed to be the same thing?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    You should not be using feof to control a loop in that way as feof only returns a true value after an attempted read has detected that the end of file condition has been reached. Furthermore, it does not make sense to open and close the same file in the loop body when you are going to append to the file anyway. I would expect something along the lines of:
    Code:
    FILE *fp = fopen("e79_resultado.txt", "a");
    if (fp)
    {
        while (fscanf(arq, "%d %d", &v1, &v2) == 2)
        {
            sum = v1 + v2;
            fprintf(fp, "%d\t%d\t%d\n", v1, v2, sum);
        }
        fclose(fp);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Because C cannot look into the future to know that the next input function will hit the end of the file.

    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Thank you very much for you help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 01:01 PM
  2. File Handling
    By m0ntana in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 03:13 PM
  3. Help! File Handling!!!
    By sevenaces in forum C++ Programming
    Replies: 12
    Last Post: 09-28-2006, 03:32 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21