scanf troubles 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 8 post ] 

 Relevant Pages 

1. printf/scanf trouble on SPARCstation

2. Scanf troubles

3. Newbie: Trouble with scanf - HELP!!!

4. Trouble with scanf() (newbie here)

5. trouble bulltproofing scanf

6. Troubles using C's scanf Function

7. trouble with scanf?

8. Trouble with scanf( "...%[^...]", ...)

9. scanf or n/scanf

10. newbie trouble: CoCreateInstance Error + IWebBrowser trouble...

11. (novice) I'm Having trouble scanf'ing a double ?

12. scanf("%s", string) or scanf("%s", &string)? Both work, yet...

 

 
Powered by phpBB® Forum Software