0

Dear all;

I have problem in reading data file in C. I have data like this:

00:00:00 R- 0.0654 345 +19
00:01:00 R+ 1.5678 324 +19
00:02:00 2.3456 315 +19
00:03:00 R- 1.2352 324 +19
........................ next until 1440 lines

What i want is only the data in third column, but as we see in second column sometimes is empty. I try read the data as string first like :
fscanf (fdin1, "%s %s %s %s %s",data1, data2, data3, data4, data5);
but in third coulumn i have trouble, because data5 = 00:03:00 not +19.

could help me how to read the data in the better way?

Thank you very much
Marzuki

2
Contributors
2
Replies
5
Views
9 Years
Discussion Span
Last Post by marzuki
0

Read each line using fgets(), as in

char buff[BUFSIZ];
while ( fgets( buff, sizeof buff, fdin1 ) != NULL ) {
  if ( sscanf( buff, "%s %s %s %s %s",data1, data2, data3, data4, data5) == 5 ) {
    // could be a line like 
    // 00:00:00 R- 0.0654 345 +19
  } else {
    // might be a line like
    // 00:02:00 2.3456 315 +19
  }
}

The key point is to read a whole line using fgets(). What you do afterwards is up to you in terms of validation and conversion.

0

Thanks very much for quick response. I`ve tried it and it`s really helpfull. Again, thanks very much....

regards;
marzuki

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.