0

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

HetaXD
  • 23
  • 5
  • Does this answer your question? [Reading a string with scanf](https://stackoverflow.com/questions/5406935/reading-a-string-with-scanf) – Tarek Dakhran Feb 22 '20 at 14:49
  • Your description of your code is not sufficient to inform an answer to the question. We need to see a [mre] demonstrating your problem to discuss any specifics of what may be wrong or how to fix it. – John Bollinger Feb 22 '20 at 14:50
  • 3
    Don't post pictures of the code, post the code in text format. – anastaciu Feb 22 '20 at 15:10
  • Don't write `perror("Cannot open file!\n")`. It is not helpful to omit the path, nor is it useful to include the newline. Much better to write `perror("supermarket.dat")`. Even better is to put the path in a variable and write `perror(path)` – William Pursell Feb 22 '20 at 15:40
  • Just post *all* the relevant code into one or more code blocks (i.e. indent it four spaces so it gets auto-formatted). It looks like I'm only seeing some of the code, that it's not well format (not styled well) and I have to read it in unusual ways in someone's personal style. Too much of a hassle. Moving on. – clearlight Feb 23 '20 at 01:14

1 Answers1

0

Use

 fgets(buffer, sizeof buffer, filehandle);

Then use

if (sscanf(buffer, "%d %f", &Intvar, &floatvar) == 2)
     // Data ready
4386427
  • 33,845
  • 4
  • 32
  • 53