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 stru
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.