Ibrahim M. Eshera
Ibrahim M. Eshera

Reputation: 25

Reading and Writing to Files in C

I'm fairly new to C. This is the first program I've written involving reading and writing to files. So far, I was able to read the file, perform the operations I need but I am having trouble with 2 things.

  1. Whatever the file is, it omits the last line when reading. For example if the file has:

    3
    5
    6
    

    It will only read the 3 and 5. But if I leave an empty/blank line at the bottom it'll read all three. Any ideas as why that is?

 

  1. I now need to take what I did, essentially converting volts to milliVolts, microVolts, etc. and write it back to the file. What I have been doing up until now is reading it from the file and working through the console. So essentially, I want write the last two printf statements to the file. I had some attempts at this but it wasn't working and I couldn't seem to find decent support online for this. When I tried, it would completely erase what was in the file before.

 

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    FILE * file = fopen ("bacon.txt", "r");
    float voltage = 0, voltageArray[100],  voltageToMilli = 0,voltageToMicro = 0, voltageToKilo = 0, voltageToMega = 0;
    int i = 1, j = 0;

    fscanf (file, "%f", &voltage);    
    while (!feof (file)) {
        printf("Line # %d\n", i);
        printf ("The voltage is: %f\n", voltage);
        voltageArray[j] = voltage;
        fscanf (file, "%f", &voltage); 

        printf("The voltage in Element %d is: %f Volts",j,voltageArray[j]);
        voltageToMilli = voltageArray[j] * 1000;
        voltageToMicro = voltageArray[j] * 1000000;
        voltageToKilo = voltageArray[j] * 0.001;
        voltageToMega = voltageArray[j] *0.000001;
        printf("\nThe voltage is %f Volts, which is: %f milliVolts, %f microVolts, %f kiloVolts, %f megaVolts",voltageArray[j],voltageToMilli,voltageToMicro,voltageToKilo,voltageToMega);


        printf("\n\n");
        i++;
        j++;



      }
    fclose (file);     

    return (0);
}

Please try to keep explanations clear and simple as I am a beginner in C. Thank you!

Upvotes: 1

Views: 122

Answers (2)

Jasen
Jasen

Reputation: 12402

problem is in the file there is nothing after the last number, so, after reading the last number from the file, feof(file) is true. and the while exits.

simplest fix is change it to this

while(fscanf (file, "%f", &voltage) == 1) {

and remove the other fscanf calls.

this works because that fscanf() call will return 1 when it is able to read a number and either 0 or EOF (which is a negative number) otherwise.

Upvotes: 1

kaylum
kaylum

Reputation: 14046

For the first issue, the problem is that the loop logic is incorrect. On each iteration is stores the previous read data, reads the next data and then goes back to the top of the loop. The problem with this is that the next data is not stored until the next iteration. But after reading the last data item (and before storing it into the array) the feof check is always false. Refer to this question for why checking feof as a loop condition is almost always wrong.

Here is an example of how you could restructure your code to read all the items as intended:

int rval;

while ((rval = fscanf(file, "%f", &voltage)) != EOF) {
    if (rval != 1) {
        printf("Unexpected input\n");
        exit(-1);
    }

    voltageArray[j] = voltage;

    /* Do the rest of your processing here. */    
}

Upvotes: 2

Related Questions