944,116 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11065
  • C RSS
Nov 14th, 2007
0

read data file in C

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marzuki is offline Offline
5 posts
since Nov 2007
Nov 14th, 2007
0

Re: read data file in C

Read each line using fgets(), as in
  1. char buff[BUFSIZ];
  2. while ( fgets( buff, sizeof buff, fdin1 ) != NULL ) {
  3. if ( sscanf( buff, "%s %s %s %s %s",data1, data2, data3, data4, data5) == 5 ) {
  4. // could be a line like
  5. // 00:00:00 R- 0.0654 345 +19
  6. } else {
  7. // might be a line like
  8. // 00:02:00 2.3456 315 +19
  9. }
  10. }
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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 14th, 2007
0

Re: read data file in C

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

regards;
marzuki
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marzuki is offline Offline
5 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 16 bit to 8 bit
Next Thread in C Forum Timeline: Factorial program using an array.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC