Search code examples
cfilewhile-loop

How to use a while loop to print each line of a fscanf until the end of the file?


The idea behind this code is to read the input file, which comprises of two digits per line, and then print it out to another file(with some minor changes etc). I was able to make the code work flawlessly using a for loop, but that only if i know how many lines there are in the file. The while loop I am using now does not work, any fixes or ideas to how I could repeat the fprintf until the end of the file?

Example Input = 1 2.3 2 5.9 3 2.7 Example Output = Customer 1 has spent 2.3*2.99 (repeated for all customers)

I simply need a way where my loop will repeat my fscanf and print statements all the way to the end of the file.

The loop I have now does not work because of a segmentation fault.

#include <stdio.h>

int main(){

    int customer1;
    double cost1;
    int x;
    double totalCost = 0;
    
    FILE * saleInput;
    FILE * saleOutput;
    
    saleInput = fopen("sales.txt","r");
    saleOutput = fopen("recordsales.txt","w");
    
    printf("Retrieving the Records from today's sale!\n");
    
    while(fscanf(saleInput,"%d",&customer1)!=EOF)
    {
        fscanf(saleInput,"%d%lf",&customer1,&cost1);
        
        cost1 = cost1*2.99;
        
        printf("Customer: %d\t", customer1); //yes a tab character was used
        printf("Spent: $%.2lf\n", cost1);
        
        fprintf(saleOutput, "Customer: %d\t", customer1);
        fprintf(saleOutput, "Spent: $%.2lf\n", cost1);

        
        totalCost += cost1;
    }

    fprintf(saleOutput, "-----------------------------\n");
    printf("-----------------------------\n");
    
    printf("$%.2lf was made today.\n", totalCost);
    fprintf(saleOutput,"$%.2lf was made today.\n", totalCost);
    return 0;
}

Solution

  • There are 4 problems:

    • you don't initialize cost1, it's initial value is undetermined, you need to initialize it to 0.
    • you're reading the customer1 value twice.
    • you don't check if your calls to fopen succeed.
    • you should explicitly check that you're reading exactly 2 values, but anyway, fscanf shouldn't be used in a real world program as it is impossible to deal with invalid input.

    I assume the format of the sales.txt file is something like this:

    10  15.55
    10  23.50
    20  100.00
    

    You want this:

      ...
      saleInput = fopen("sales.txt", "r");
      if (saleInput == NULL) // << add this
      {
        printf("Cannot open sales.txt\n");
        return;
      }
    
      saleOutput = fopen("recordsales.txt", "w");
      if (saleOutput == NULL) // << add this
      {
        printf("Cannot open recordsales.txt\n");
        return;
      }
    
      printf("Retrieving the Records from today's sale!\n");
      
      cost1 = 0;  // << add this
    
      while (fscanf(saleInput, "%d %lf", &customer1, &cost1) == 2) // while we read exactly 2 values
      {
        cost1 = cost1 * 2.99;
        ...