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:

if ( sscanf( buffer, "%d %41[^;] %51s", &pMovie->year, &pMovie->title,
				&pMovie->director ) == 3 )

But the separation doesn't work the way I want it to.

Recommended Answers

All 2 Replies

How about something like:

int main(void)
{
    char buffer[] = "1941 How Green Was My Valley; John Ford." ;
    int year = 0 ;
    char title[BUFSIZ] = { '\0' } ;
    char director[BUFSIZ] = { '\0' } ;
    if ( sscanf( buffer, "%d %[^;]; %[^.]", &year, &title, &director ) == 3 )
        printf( "Year = %d, title = %s, diretro = %s", year, title, director ) ;
    getchar( ) ;
}

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 %[^;]

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.