parsing with sscanf

Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

parsing with sscanf

 
0
  #1
Nov 8th, 2006
I'm having trouble parsing a line with sscanf.

My line is:

1941 How Green Was My Valley; John Ford

and I want to separate it into a number, string, and another string. I tried:
  1. if ( sscanf( buffer, "%d %41[^;] %51s", &pMovie->year, &pMovie->title,
  2. &pMovie->director ) == 3 )

But the separation doesn't work the way I want it to.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,610
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: parsing with sscanf

 
0
  #2
Nov 8th, 2006
How about something like:
  1. int main(void)
  2. {
  3. char buffer[] = "1941 How Green Was My Valley; John Ford." ;
  4. int year = 0 ;
  5. char title[BUFSIZ] = { '\0' } ;
  6. char director[BUFSIZ] = { '\0' } ;
  7. if ( sscanf( buffer, "%d %[^;]; %[^.]", &year, &title, &director ) == 3 )
  8. printf( "Year = %d, title = %s, diretro = %s", year, title, director ) ;
  9. getchar( ) ;
  10. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: parsing with sscanf

 
0
  #3
Nov 8th, 2006
yeah that's what I want, but how do you get the string after the semi-colon if there was no period at the end?

and why do you need a semi-colon after %[^;]
Last edited by degamer106; Nov 8th, 2006 at 6:07 pm.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC