944,067 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4108
  • C RSS
Nov 8th, 2006
0

parsing with sscanf

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Nov 8th, 2006
0

Re: parsing with sscanf

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. }
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 8th, 2006
0

Re: parsing with sscanf

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006

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: to ReverseString using c language
Next Thread in C Forum Timeline: How do you create a text file?





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


Follow us on Twitter


© 2011 DaniWeb® LLC