1

The program I am making is supposed to read in numbers from a text file and save the total number of numbers, the average value of the numbers in a struct.

I have a struct that looks like this:

struct seriepost {
    int totnr;   
    int outnr;   
    float average;  
};

And the function (unfinished) looks like this:

int read_data(FILE *tsin, struct seriepost serie[]) {   

        int x = 0;
        float average = 0;
        float in_last = 0;
        while (!feof(tsin))
        {
            while (fscanf(tsin, "%f", &in_last) != 0.0)
            {
            serie[x].totnr += 1;
            serie[x].medel = average/serie[x].totnr;
            serie[x].outnr = average*1.05+average*0.95;
            }
            x += 1;
        }
        fclose(tsin);
        return sizeof(serie);
    }

The text file looks like this:

22.2 12.4 24.5 12.4..... 
22.2 12.2 0.0

2.21 12.1 11.1 11.1.... 
1.1 0.0 

Where 0.0 marks the end of a series.

Now i want the fscanf to read all the numbers until 0.0 then i want it to skip to the next array spot for the next series. So i have like serie[0], serie[1] with their own set of numbers and averages values etc.

1
  • +1. Excellent use of resources available on the web! -- Thomas, who is the teacher of the course where this was assignment number 7. Commented Aug 30, 2013 at 8:25

2 Answers 2

2

If you see a reference of fscanf, you will see that it returns the number of items successfully scanned, and not the value you just scanned.

That means you have to check differently for your ending condition.

You also should not loop while (!feof(...)), as the end-of-file condition will not be set until you already tried to read beyond the end of the file.

Instead you could do something like this:

while (fscanf(tsin, " %f", &in_last) > 0)
{
    if (in_last == 0.0f)
        ++x;
    else
    {
        /* The rest of your code */
    }
}

This will read until you reach end-of-file, or there is an error, and increase the index once you read 0.0. The leading space in the fscanf format string make fscanf skip whitespace (spaces, tabs, newlines).


There are also a couple of other problems with your code. For example, you donät update the average variable. The value of serie[x].totnr may not be initialized, and so increasing it (and using it in operations in general) will in that case be undefined as well. And then you use sizeof(series) which will not work, as series is not actually an array in the function but a pointer, so the sizeof operator returns the size of the pointer and not the number of entries in the array. Instead return x which is now the size of the array.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh i completely misunderstood how fscanf worked. Your code is really good and easy to understand makes me feel stupid. Thanks for the help!
0

fscanf returns the number of successfully read items, not the value.

while (fscanf(tsin, "%f", &in_last) != 0.0)

should be:

while (fscanf(tsin, "%f", &in_last) != 1)

Additionally you can not compare a float against a constant value. That doesn't work in most cases. So you need to choose a delta, and then check if your float is within that range.

I think you also don't want to return sizeof(serie) because this is a constant value you already know when calling. More likely you want to return x-1 to indicarte the number of values read. If the feof condition is met, you haven't read anything and x will be 1 (because of your loop).

BTW: Your program might have a problem if you don't have a fixed size of lines in your textfile and know beforehand how big the array must be. You don't allocate the array while reading, but you pass it alerady in, so when x increases, you might overrun the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.