Search code examples
carraysfgets

How to get data from fgets()


I have a data file with information like this:

    3 10.9
    1 2.1
    10 100.5
                               //This is a blank line
    10 200

The first is an integer and the second is a float data. It also needs to check whether a blank line exist. So I use a float x[20] array to contains it and use fgets() to get the value of each line. But how can I get back these values as printf("%d%f",x[0],x[1]); can't get back the value I wanted, it gives some strange values.The output looks like this


Solution

  • Use

     fgets(buffer, sizeof buffer, filehandle);
    

    Then use

    if (sscanf(buffer, "%d %f", &Intvar, &floatvar) == 2)
         // Data ready