Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

formatted input C

Name: Anonymous 2012-10-05 6:05

hey guys,

a little C help needed.

how would you go about reading in a string like:
string1#string2#integer
which would read string1 and string2 into 2 different strings and the integer into an integer?

tried to do it with
scanf("%s%s%d",a,b,&c);
but it doesnt work that way :(

Name: Anonymous 2012-10-05 6:08

scanf the whole line, then strtok it by #.

Name: Anonymous 2012-10-05 6:30


#include <stdio.h>

int main(void)
{
    char line[1024], s1[1024], s2[1024];
    int d;

    while (fgets(line, sizeof line, stdin)) {
        if (sscanf(line, "%[^#]#%[^#]#%d", s1, s2, &d) != 3)
            continue;
        printf("%s %s %d\n", s1, s2, d);
    }
    return 0;
}

Don't change these.
Name: Email:
Entire Thread Thread List