Author |
Message |
Ben Goo #1 / 8
|
 scanf troubles
When I use scanf with the following code: scanf("%s*END*", buf); /*buffer is a char * properly initialized*/ and I enter the text "sometext*END*", buf is filled with the whole string *END* included. Is scanf supposed to behave this way or is there something I'm not doing correctly. I would like it to read a character array until *END* is encountered. Any input would be greatly appreciated. Thanks, Ben
|
Sat, 01 Jan 2005 00:26:32 GMT |
|
 |
Ben Pfaf #2 / 8
|
 scanf troubles
Quote:
> When I use scanf with the following code: > scanf("%s*END*", buf); /*buffer is a char * properly initialized*/ > and I enter the text "sometext*END*", buf is filled with the whole string > *END* included. Is scanf supposed to behave this way or is there something > I'm not doing correctly.
scanf() is working as designed here. `%s' causes characters to be read into the supplied buffer up to the first white-space character. The characters following `%s' in the format string do not affect its termination characteristics. Quote: > I would like it to read a character array until *END* is > encountered. Any input would be greatly appreciated.
I don't think that is possible with scanf(). You could use `%[^*]' to match characters up to the first asterisk. -- "When I have to rely on inadequacy, I prefer it to be my own." --Richard Heathfield
|
Sat, 01 Jan 2005 00:48:31 GMT |
|
 |
Jirka Klau #3 / 8
|
 scanf troubles
Quote:
> When I use scanf with the following code: > scanf("%s*END*", buf); /*buffer is a char * properly initialized*/ > and I enter the text "sometext*END*", buf is filled with the whole string > *END* included. Is scanf supposed to behave this way or is there something > I'm not doing correctly. I would like it to read a character array until > *END* is encountered. Any input would be greatly appreciated.
This is not easy with scanf. You could use fgets and strstr instead. fgets(buf, LEN, stdin) && (p = strstr(BUF, "*END*")) && (*p = 0); Jirka
|
Sat, 01 Jan 2005 01:04:29 GMT |
|
 |
Jirka Klau #4 / 8
|
 scanf troubles
Quote: > fgets(buf, LEN, stdin) && (p = strstr(BUF, "*END*")) && (*p = 0);
^^^ This is buf, of course. And p ist a char*, obviously. :-) Jirka
|
Sat, 01 Jan 2005 01:11:52 GMT |
|
 |
Eric Sosma #5 / 8
|
 scanf troubles
Quote:
> When I use scanf with the following code: > scanf("%s*END*", buf); /*buffer is a char * properly initialized*/ > and I enter the text "sometext*END*", buf is filled with the whole string > *END* included. Is scanf supposed to behave this way or is there something > I'm not doing correctly. I would like it to read a character array until > *END* is encountered. Any input would be greatly appreciated.
If "any input" is what you want, just use fgets() ... Seriously, I don't think there's any way to get scanf() to do this. The %s directive matches any sequence containing no white-space characters (after skipping any initial white space), so it will happily consume the "*END*" characters. The %[ directive could be used to recognize a single-character end-of-input delimiter, but I don't think you could get it to do the kind of backtracking you'd need to process inputs like "***EASY CREDIT TERMS*** *END*". Depending on the larger context of your problem, you may be best served by reading an entire line with fgets() and then picking it apart with the str...() functions. In particular, strstr() is an easy way to search for the "*END*" string. If you must not consume any characters beyond the final asterisk (e.g., if "Data1*END* Data2*END* Data3*END*" is all on one input line) you'll need to work noticeably harder. --
|
Sat, 01 Jan 2005 00:54:48 GMT |
|
 |
Mark McIntyr #6 / 8
|
 scanf troubles
On Mon, 15 Jul 2002 12:26:32 -0400, in comp.lang.c , "Ben Good" Quote:
>When I use scanf with the following code: > scanf("%s*END*", buf); /*buffer is a char * properly initialized*/ >and I enter the text "sometext*END*", buf is filled with the whole string >*END* included. Is scanf supposed to behave this way or is there something >I'm not doing correctly.
scanf doesn't work like you think it works. It scans a line searching for an object whose type matches each of your specified formats. Unfortunately "sometext*END*" matches a %s, and so thats what you get in the %s. Quote: >I would like it to read a character array until >*END* is encountered. Any input would be greatly appreciated.
Don't use scanf. See the FAQ for some reasons. Try using fgets and then parsing the line with strtok or something similar. -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Sat, 01 Jan 2005 06:07:47 GMT |
|
 |
Peac #7 / 8
|
 scanf troubles
Quote:
> When I use scanf with the following code: > scanf("%s*END*", buf); /*buffer is a char * properly initialized*/ > and I enter the text "sometext*END*", buf is filled with the whole string > *END* included. Is scanf supposed to behave this way or is there something > I'm not doing correctly. I would like it to read a character array until > *END* is encountered. Any input would be greatly appreciated.
don't use scanf. I hope you are not averse to using fgets(). Use fgets() with stdin as the stream.the you can scan the dtring inout using strstr() or if you want to parse it use strtok().
|
Sat, 01 Jan 2005 14:06:01 GMT |
|
 |
Dan P #8 / 8
|
 scanf troubles
Quote: >When I use scanf with the following code: > scanf("%s*END*", buf); /*buffer is a char * properly initialized*/ >and I enter the text "sometext*END*", buf is filled with the whole string >*END* included. Is scanf supposed to behave this way or is there something >I'm not doing correctly.
%s will cause scanf to read anything up to the first whitespace character. Because of this, you *must* insert a maximum length in the conversion specification, otherwise the user may overflow the buffer that receives the input. Quote: >I would like it to read a character array until >*END* is encountered. Any input would be greatly appreciated.
The trivial fix is to use " *END*" instead of "*END*" as the end of input marker: rc = scanf("%25s *END*", buf); This will behave exactly as you want, as long as the user input (until the space) does not exceed 25 characters. If it does, the extra characters and the end of input marker will remain buffered in the stdin stream, ready for the next input operation from stdin (which may not be exactly what you want). The simplest way to protect yourself against this possibility is to read a full line of input from stdin and parse it afterwards with sscanf, as described above. Of course, you can simplify the rules of the game and use the newline character (it counts as white space, too) as the end of input marker. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Sat, 01 Jan 2005 20:04:32 GMT |
|
|
|