Monday, May 24, 2010

Question about strings in C?

When reading in strings, how can I include spaces in the string?





What I mean is that, normally, you'd need something like





scanf("%s%s%s", %26amp;str[0], %26amp;str[1], %26amp;str[2])





to take something like





"one two three"





and read it in as three strings.





How can I read in "one two three" as one string? That is, without the program assuming that spaces are breaks between strings?

Question about strings in C?
You can use scanf as below to read until end of line.





scanf("%[^\n]",%26amp;str[0]);





That should work.





All the best !
Reply:fgets gets the entire line.


If you want to read in more than a line you must use fread.


Then parse it manually.





P.S.


I personally don't use scanf to read in strings from stdin because it's a potential buffer overflow(security risk).


If you are going to use scanf specify the field width %10s or use %as to allocate the string.ualifier is present, the next pointer must be a pointer to


wchar_t, in


No comments:

Post a Comment