0

I have a text file containing datas that I want to read and store in a C structure. But this file can be different with a variable number of columns and rows, depending on the case being treated.

For example in one case we have this file (3 columns, 4 rows) :

56.3 1.8 25.9

-8.0 3.2 156.7

-9.4 -12.36 -11.8 

-0.27 -0.04 0.1004

And in an other case we could have (2 columns, 5 rows) :

2.3 65.7 

-8.2 3.4 

9.4 -12.76 

-0.27 -0.04 

 0.56  45.8

I can't use fscanf because I don't know at each cases the number of datas I will read. With fgets I can't access to each datas separately. For example I would like to store datas like this : datas[0][0] = line[0];

Do you have any idea to perform a such reading in C ?

  • 1
    You can use `fgets` and then `sscanf` one value at a time though – Ted Lyngmo Dec 20 '21 at 10:18
  • 1
    My favorite pattern: (1) use `fgets` (or the equivalent) to read lines of text. Then, (2) use `strtok` (or the equivalent) to break each line up into whitespace-separated "words". Finally, if necessary (3) use `atoi` or `atof` to convert strings to numbers, or use `strdup` to save safe copies of strings, etc. See also [this question](https://stackoverflow.com/questions/58403537). See also [these course notes](https://www.eskimo.com/~scs/cclass/notes/sx10h.html). – Steve Summit Dec 20 '21 at 11:15

0 Answers0