reading a string with spaces with sscanf

Posted by SDLFunTimes on Stack Overflow See other posts from Stack Overflow or by SDLFunTimes
Published on 2010-05-18T04:03:53Z Indexed on 2010/05/18 4:10 UTC
Read the original article Hit count: 431

Filed under:
|
|
|

For a project I'm trying to read an int and a string from a string. The only problem is sscanf appears to break reading an %s when it sees a space. Is there anyway to get around this limitation? Here's an example of what I'm trying to do:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char** argv) {
    int age;
    char* buffer;
    buffer = malloc(200 * sizeof(char));
    sscanf("19 cool kid", "%d %s", &age, buffer);

    printf("%s is %d years old\n", buffer, age);
    return 0;
}

What it prints is: "cool is 19 years old" where I need "cool kid is 19 years old". Does anyone know how to fix this?

© Stack Overflow or respective owner

Related posts about c

    Related posts about sscanf