Thread: C file handling

  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
    28,412
    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);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    945
    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, 02: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