-3

I have a file consist of 6 columns of data. I would like to retrieve the second and the last last three data. The last three data need to be stored in a 3 dimension array,

Here is example of the data in files:

gly  1 A 12.11 13.14 14.14
asp 2 A 13.23 24.64 35.25
glu 3  B  32.45 11.45 54.86
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zzprog
  • 113
  • 2
  • 11
  • 3
    Use `fgets` to read each line. Use `sscanf` to parse each line. See [this](https://stackoverflow.com/a/36731485/3386109) for example. – user3386109 May 20 '16 at 17:23
  • Are you really sure you want a 3 dimension array? Each line has three items of data to put in an array, but whether 3, 4, or 10 items, I make that a 2 dimension array. What have you tried? – Weather Vane May 20 '16 at 17:49
  • because the last three data is the coordination of the atom in 3d. so i want to store the coordination for all atom in an array – zzprog May 20 '16 at 22:43
  • ***Show Code*** – abelenky Jan 22 '17 at 23:00

1 Answers1

1

Read each line with your preferred function (fgets, fscanf, ...) and parse the line with sscanf.

char buffer[50];
char token1[15];
int token2;
char token3;
float token4, token5, token6;

while(fgets(buffer, 49, file)) {
    sscanf(buffer, "%s %d %c %f %f %f", token1, &token2, &token3, &token4, &token5, &token6);
    ...
}

Also, be careful with the return value of sscanf.

ddz
  • 526
  • 3
  • 15