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

Recommended Answers

All 2 Replies

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.

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

regards;
marzuki

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.