Read data into a float array in C

10,676

Solution 1

To read the data from the file use fgets() and sscanf().

/* needs validation and error checking */
fgets(buf, sizeof buf, fp);
sscanf(buf, "%f", &tmp);

To manage the number of floats, you have two options.

  1. use an array of fixed size
  2. use malloc(), realloc() and friends for a growable array

/* 1. use an array of fixed size */
float array[1000];

/* 2. use `malloc()`, `realloc()` and friends for a growable array */
float *array;
array = malloc(1000 * sizeof *array);
/* ... start loop to read floats ... */
    if (array_needs_to_grow) {
        float *tmp = realloc(array, new_size);
        if (tmp == NULL) /* oops, error: no memory */;
        array = tmp;
    }
/* end reading loop */
/* ... use array ... */
free(array);

Solution 2

Not sure what else you need, but this solves the essence of the problem:

int j = 0;
double flts [20000];
while (!feof(f))
{
    if (fscanf (f, "%g", &flts [j]) == 1) 
        ++j; // if converted a value okay, increment counter
}

Solution 3

you can't unless your data file is in binary fromat. You are showing a plaint text file: in order to get that into an array, you will need to do a couple of things: find out how much items needed in the array (number of lines?), allocate the array, then convert and put each value in the array. Conversion can be done using scanf for example.

Solution 4

You can't use fread as that doesn't parse the form of file that you have. fread just reads bytes into an array of bytes and what you have is numbers written out in a human readable decimal form.

I think that for this format the most direct way that you can use is to use fscanf in a loop, being sure to check the return value and, on failure, ferror and/or feof to check whether you hit the end of file or some other error.

Share:
10,676
sfactor
Author by

sfactor

Dreamer, Analyst, Engineer, Programmer, Photographer.

Updated on June 05, 2022

Comments

  • sfactor
    sfactor almost 2 years

    i am making a program where i need to load some floating point data of the format from a file
    103.45
    123.45
    456.67
    ......

    i was wondering how to store these data directly into a array of floating point numbers using fread(). i guess it is not that hard using pointers but i am not so good with them. can anyone tell me how

  • Hassan Syed
    Hassan Syed over 14 years
    All else being sane, he can cast the the float array into a byte* required by fread.
  • CB Bailey
    CB Bailey over 14 years
    He could do, but it wouldn't work because it would only fill a float array with valid values if the input file was a binary dump made in a similar style with an fwrite. What sfactor has described is a one float per line text file in a human readable decimal format.
  • sfactor
    sfactor over 14 years
    no the file is a text file with these values in each line. i have gotten a far as opening the file and getting the filesize, now how do i allocate memory to the floating point array and read them one at a time from the text file.
  • sfactor
    sfactor over 14 years
    thanks to everyone for the answers...i managed to get the values into a floating point array....but now the problem is that the values in the array have extra precision, e.g 107.94 is stored as 107.940002 105.76 is stored as 105.760002 105.22 is stored as 105.220001 where is this extra precision coming from?...the actual data has only precision of 2 decimal points. i tried doing fscanf (f, "%0.2f", &data [i])....but this does not work?
  • wallyk
    wallyk over 14 years
    If it was loaded into an array of float then displayed using say %10g, that is the kind of noise you'd see from lack of precision. floats have have 6 to 7 significant digits of precision. doubles have 15 to 16 significant digits of precision.