read data file in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 5
Reputation: marzuki is an unknown quantity at this point 
Solved Threads: 0
marzuki marzuki is offline Offline
Newbie Poster

read data file in C

 
0
  #1
Nov 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: read data file in C

 
0
  #2
Nov 14th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: marzuki is an unknown quantity at this point 
Solved Threads: 0
marzuki marzuki is offline Offline
Newbie Poster

Re: read data file in C

 
0
  #3
Nov 14th, 2007
Thanks very much for quick response. I`ve tried it and it`s really helpfull. Again, thanks very much....

regards;
marzuki
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC