Thread: Help for File Handling

  1. #1
    Registered User
    Join Date
    May 2016

    Angry Help for File Handling

    QUESTION : Consider the file expenses.txt with the following information:
    Write a code segment that opens the file, reads the file and computes the sum of all the expenses, prints the sum. Here is part of the code. Fill in the missing code so it performs the required task.

    int main( void )
    /* declarations */
    int item, sum;
    FILE* infile;
    /* initialize sum */
    /* open the file for input */
    /* read the first data value */
    while ( item>=0 )
     /* process the value */
     /* read the next data value */
    /* print the final sum */
    /* close the file */
    return 0;
    i made that from a different way in below. But i wanna make this question like above. I dont understand how works the while loop in this code. Thanks for your helps.

    #include <stdio.h>
    int main()
    FILE *infile;
    int x=22;
    int y=167;
    int z=33;
    int a=-1;
    int sum;
    sum = x+y+z+a;
    infile = fopen("expenses.txt","w");
    fputs( "Show the sum of the numbers!\n",infile );
    fprintf(infile,"%d + %d + %d+ %d = %d",x,y,z,a,sum);
    puts("Saved, please continue.");

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    You didn't explain what your difficulties are in much detail, so I have no idea if the following will actually help you.

    So your version of the program has a number of problems. Most significantly, you don't follow instructions. The program doesn't read any values from the file, and instead of printing the sum for the user, you write it to the file, overwriting the original data. Other problems are that you create variables x, y, z and a which have fixed values, your program wont work if the contents of expenses.txt change.

    At a high level, the while loop is reading and summing values from expenses.txt until it is told to stop. In this case, it's any negative value that signals the program to stop summing expenses. In programming lingo, such special values are often called sentinel values.
    Breaking it down a bit, The while loop checks the value of the item variable and repeats if the value is greater than or equal to zero. It doesn't show it, since you are supposed to write the code yourself, but item will contain the values read from expenses.txt file. So from the sample examples.txt you provided, item will contain 22, then 167, then 33, then -1. When it reads the -1, the loop will stop and you will print the sum, close the file and exit.

    Check out our tutorial on file handling here for a very basic intro (any course notes and texbook you may have will also be helpful). You'll want to open the file as read-only. You'll probably want to use fscanf to read a value into the item variable in the places where the comments say something about reading a data value. It looks like you're reading integer data, so pick the right format specifier. Also, make sure you initialize your variables to sensible values before you use their values. Sometimes the nature of the code does this (e.g. you call fopen and get a value for infile before you use infile in fscanf). Sometimes, you need to do this yourself, as is the case with sum.

    Then, take another attempt at this problem. If you're still stuck, post your code and explain in more detail what exactly it is you're having trouble with. What compiler errors are you getting (copy-paste exactly, with line numbers please)? What input are you giving it and what output are you getting? What should the correct output be?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling:
    By jocdrew21 in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2013, 04:32 PM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  3. FILE Handling
    By Drainy in forum C Programming
    Replies: 8
    Last Post: 10-13-2009, 01:13 PM
  4. file handling
    By vaibhav in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 12:12 PM

Tags for this Thread