-
sscanf()
with sscanf() if I want to pick up strings do I allow for the '\n'
i.e. if i want to pick up 2 strings of 20 characters & 15characters
would it be sscanf( string, "%20s%15s", string1, string2)
or
sscanf( string, "%21s%16s", string1, string2 )
Thanks
-
Well %s will ignore / skip over any newline characters
If you have
char string1[20];
Then the max field width for conversion would be
"%19s"
Making the conversion size automatically dependent on the buffer size is more difficult than it ought to be.
-
It is:
char string1[21]; /* 20 characters plus newline character */
I have allowed for the newline character when I created it.
what I was unsure of is to pick them out of a temporarty string would it be:
sscanf( temp_string, "%20s", string1 );
-
> char string1[21]; /* 20 characters plus newline character */
No, 20 chars + a nul (\0)
Like I just said, "%s" will NOT copy a \n character even if one is present.
If you want to read a string including a \n, then use fgets()
-
sorry I am just being stupid now. I didn't mean newline I ment null