First, I want to make clear that I'm a beginner and this might be a silly question and that I'm probably doing something wrong.
I want to read characters from a string until a ,
is found and store them in another string. I also want to know how many characters have been read. This is what I'm doing with sscanf
:
sscanf(str, "%[^,]s %n ", newstr, &number);
When I try to print number it prints 0
regardless of my input, even when several characters were stored in newstr
.
The problem seems to be in the [^,]
sub-specifier as %n
works as it should without it.
I want to read characters from a string until a ',' is found and store them in another string. I also want to know how many characters have been read.
The s
is not needed. It is not part of the "%[^,]"
specifier. Also the trailing " "
serves no purpose. Should limit input length too. Do not use newstr
unless code knows it was filled.
char str[100];
int number = 0;
// sscanf(str, "%[^,]s %n ", newstr, &number);
sscanf(str, "%99[^,], %n", newstr, &number);
if (number) Success();
else Fail(); // do not use newstr