1

I'm trying to sort a string into different variables in C (when I say string I mean char array). The string contains smaller 'blocks', separated by a tab, and I want sscanf to put the next 'block' into a string each time it is called.

For example, if the string is 00:00:00:00 12345 abcd, I want the first sscanf call to put 00:00:00:00 in a string, the next sscanf call to put 12345 in the string, etc.

I can't understand why my code doesn't work. I have sscanf(s, "%[^\t]%*c", buf), which (I think) should tell sscanf to read everything up to a tab character, and then the %*c tells it to read but discard the tab character (so that the next time sscanf is called, there is no leading tab character). There is never more than 1 tab character separating the blocks in the string.

I'm sure there is a better way of doing this than using sscanf, and that'd be great if someone told me, but I'd also like to figure this out too because it seems to be annoyingly easy and I don't know why it's not working. Thanks for your help

2
  • 3
    scanf() is evil, don't use it, it's an especially bad idea to use it for tokenizing. strchr(), strstr() and strtok_r() are all better alternatives. Commented Jul 24, 2013 at 12:35
  • Nothing is wrong with scanf(). Please provide a code sample which indicates a reasonable attempt at using scanf() and I will help you get it to work. Commented Aug 4, 2013 at 23:28

2 Answers 2

3

Have a look at strtok for an easier way to split it.

Also note that your sscanf call will not modify the string, just extract from it (i.e. on the 2nd call you can't just pass s back to it without first modifying it.) To do it with sscanf something like this would do it:

while (/* do your own check on str here*/)
{    
    // Scan the next part
    sscanf(str, "%[^\t]%s", firstBit, remain);

    // Copy it back and do it again
    strcpy(str, remain); // Being careful here not to sscanf into the source string!!
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. I know it won't modify it, I just thought that the %*c will get the next call of sscanf to start after the tab character
No, it won't affect the next call - if it was a scanf it might do...because you're consuming the user input, but a string is the same each time.
1

sample code:

#include <stdio.h>

int main(){
    char s[] = "00:00:00:00\t12345\tabcd";
    char buf[32];
    int pos = 0, read_len;

    //%n : reading character count into read_len address
    //pos : next read position
    while(1==sscanf(s + pos, "%[^\t]%n%*c", buf, &read_len)){
        printf("%s\n", buf);
        pos += read_len + 1;
        if(s[pos] == '\0') break;
    }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.