1

Is there a way to control how scanf() separates input?

scanf("%10s %10s", array1, array2);

The above works fine, separating by space. But, can I use a different separation signal?

Because neither of these seem to work if I want to splice input with a comma rather than a space:

scanf("%10s,%10s", array1, array2);
scanf("%10s, %10s", array1, array2);

What gives? A book I am reading claims that one can separate input with scanf() using any character, but fails to explain how to do so. Furthermore, if alternate characters can be used, can a broader criteria than a single character be used, too? (i.e. value types, statements, etc.)

3 Answers 3

2

Using character classes:

#include <stdio.h>

int main(int argc, const char **argv) {
  char s1[10], s2[10];
  const char *str = "word1,word2";
  sscanf(str, "%[^,],%s", s1, s2);
  printf("%s -- %s\n", s1, s2);
  return 0;
}

Or you can be even more specific:

sscanf(str, "%[^,],%[^,]", s1, s2);

which will also capture white space in s2

To split on multiple characters you can use strstr:

#include <stdio.h>
#include <string.h>
int main(int argc, const char **argv) {
  const char *str = "word1fooword2fooword3", *foo = "foo", *ptr;
  const char *eofstr = str;
  for (ptr = str; eofstr; ptr = eofstr + strlen(foo)) {
    char word[10];
    eofstr = strstr(ptr, foo);
    if (eofstr) {
      size_t len = eofstr - ptr;
      strncpy(word, ptr, len);
      word[len] = 0;
      printf("%s\n", word);
    } else {
      strcpy(word, ptr);
      printf("%s\n", word);
    }   
  }
  return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

This works with "word1, word2" while I think it shouldn't ("I want to splice input with a comma rather than a space"). This actually use a comma and an arbitrary amount of whitespace as delimiter, but maybe I'm misunderstanding the question.
@perreal ... experimentation suggests that using [^] allows only a single character to cue splicing the input. Is that true, or is there a way to using a broader criteria? For instance, the word 'foo' instead of ','.
Correct, this can only split at a single character. For multiple characters you can use something like strstr and a loop.
@perreal I am curious what an implementation of that would look like. If you don't feel like bothering, I understand, but you certainly have my curiosity piqued!!!
1

You actually need to change the scanf's default delimeter. And here is the exact answer to your questions.

http://gpraveenkumar.wordpress.com/2009/06/10/how-to-use-scanf-to-read-string-with-space/

4 Comments

I see how that applies to when one is assigning to a single variable, but I am struggling to see how that can be used to modulate assignment to many variables. Could you provide an example of how to correctly use that syntax to do this?
actually your question is not that clear ... can you explain a bit more with some example. Do you need to input multiple variables with different delimiters?
@perreal has provided a working example. Thanks for the link, though!
1) examples in that page are wrong (spaces before string specifier don't do anything and it's "%[]", not "%[]s", it's even in comments) 2) you can edit your comments instead of write new ones 3) let people choose if your answer is useful. :)
1

You can 'escape' the character delimiter using it between rect parentesis, but to be honest, I've never used that solution. Instead, you can use fgets and sscanf. Is a more 'solid' way to do it, in my opinion. fgets can read from the stdin and sscanf can look for commas and any other characters in the string. also, fgets returns null with ctrl+c and sscanf returns the number of successful reads. Hope this helps.

1 Comment

Thanks, I will look into those alternatives. However, the escape command is what I was seeking!

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.