Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf problem with intaking a string *PLS. HELP*

Reply
Thread Tools

scanf problem with intaking a string *PLS. HELP*

 
 
Radith
Guest
Posts: n/a
 
      07-31-2005
HI all;

I have created a program just to get an understanding of strcpy. (still an
amateur developer).

I use scanf("%s", &first) in order to get the input to the variable first
(which is char first[]).

Then I go strcpy(second, first). This is just to demonstrate the strcpy
function for myself and it (as far as I know) copies what's in first to
second.

The problem is: * When a user uses spaces in his/her input (e.g. "Hello
World")
--> Only "Hello" will be displayed.

Now I have made sure the array has ample size; I just dont know what's
wrong?

I unfortunately have deleted the source code by mistake (as I was
frustrated); so I can't supply it here; If y'all really need it I wouldn't
ming writing it out again but I think y'all can have a pretty good
understanding from my descriptions.

Please Help. Thanks a lot for it in advance.


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      07-31-2005
"Radith" <(E-Mail Removed)> writes:

> I use scanf("%s", &first) in order to get the input to the variable first
> (which is char first[]).
>
> Then I go strcpy(second, first). This is just to demonstrate the strcpy
> function for myself and it (as far as I know) copies what's in first to
> second.
>
> The problem is: * When a user uses spaces in his/her input (e.g. "Hello
> World")
> --> Only "Hello" will be displayed.


This is because %s only reads a single word. It will skip
leading white space, but after that white space causes it to stop
reading.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      07-31-2005

"Radith" <(E-Mail Removed)> wrote
>
> I use scanf("%s", &first) in order to get the input to the variable first
> (which is char first[]).
>

scanf("%s", ptr);

will only read the first word (string of characters separated by whitespace)
into the bufer pointed to by ptr.
You can read a whole line with gets(). Unfortunately the function is flawed,
because the buffer will overrun, probably causing a crash, if the user types
in too many characters. fgets() is even worse if used incorrectly - input is
silently truncated. Why is that worse? Because wrong results are usually
worse than no results. However you can build a solid input function on top
of fgets(), but it is needlessly complicated.
So regrettably there is no easy way of getting a line from the user in ISO
C.



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-01-2005
"Malcolm" <(E-Mail Removed)> writes:
> "Radith" <(E-Mail Removed)> wrote
>>
>> I use scanf("%s", &first) in order to get the input to the variable first
>> (which is char first[]).
>>

> scanf("%s", ptr);
>
> will only read the first word (string of characters separated by whitespace)
> into the bufer pointed to by ptr.
> You can read a whole line with gets(). Unfortunately the function is flawed,
> because the buffer will overrun, probably causing a crash, if the user types
> in too many characters. fgets() is even worse if used incorrectly - input is
> silently truncated. Why is that worse? Because wrong results are usually
> worse than no results. However you can build a solid input function on top
> of fgets(), but it is needlessly complicated.
> So regrettably there is no easy way of getting a line from the user in ISO
> C.


fgets() is worse than gets()?

Nonsense.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Martijn
Guest
Posts: n/a
 
      08-01-2005
> [snipped] If y'all really need it [snipped]

Is that your truck in MUD95?

Alright, alright, I'll start contributing serious responses again.

--
Martijn
http://www.sereneconcepts.nl


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      08-01-2005
Keith Thompson wrote:
> "Malcolm" <(E-Mail Removed)> writes:
>
>>"Radith" <(E-Mail Removed)> wrote
>>
>>>I use scanf("%s", &first) in order to get the input to the variable first
>>>(which is char first[]).
>>>

>>
>>scanf("%s", ptr);
>>
>>will only read the first word (string of characters separated by whitespace)
>>into the bufer pointed to by ptr.
>>You can read a whole line with gets(). Unfortunately the function is flawed,
>>because the buffer will overrun, probably causing a crash, if the user types
>>in too many characters. fgets() is even worse if used incorrectly - input is
>>silently truncated. Why is that worse? Because wrong results are usually
>>worse than no results. However you can build a solid input function on top
>>of fgets(), but it is needlessly complicated.
>>So regrettably there is no easy way of getting a line from the user in ISO
>>C.

>
>
> fgets() is worse than gets()?
>
> Nonsense.


Well, in a way, it is: It is the closest you can get to a sensible
text input function but falls short, unnecessarily and nastily.
If you consider a function more broken which does not invoke UB
(and quite often segfaults to tell you where you went wrong)
instead of silently truncating and upsetting everything in a
hard-to-catch way, then you can arrive at this conclusion.
IMO, fgets() with sensible return values to communicate, say
EOF/Error/NoNewLineEncountered/NewLineEncountered, would be
quite alright.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Mark F. Haigh
Guest
Posts: n/a
 
      08-01-2005
Michael Mair wrote:
> Keith Thompson wrote:


<snip>

> >
> >
> > fgets() is worse than gets()?
> >
> > Nonsense.

>
> Well, in a way, it is: It is the closest you can get to a sensible
> text input function but falls short, unnecessarily and nastily.
> If you consider a function more broken which does not invoke UB
> (and quite often segfaults to tell you where you went wrong)
> instead of silently truncating and upsetting everything in a
> hard-to-catch way, then you can arrive at this conclusion.

<snip>

Huh? UB nearly universally "[upsets] everything in a hard-to-catch
way". Even worse, it often results in security holes.

Well-defined but annoying behavior is certainly not worse, at least in
the sense of the word I am accustomed to.


Mark F. Haigh
(E-Mail Removed)

 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      08-01-2005
On Mon, 01 Aug 2005 08:51:23 +0200, Michael Mair
<(E-Mail Removed)> wrote:

> Keith Thompson wrote:
>> "Malcolm" <(E-Mail Removed)> writes:
>>
>>>"Radith" <(E-Mail Removed)> wrote
>>>
>>>>I use scanf("%s", &first) in order to get the input to the variable first
>>>>(which is char first[]).
>>>>
>>>
>>>scanf("%s", ptr);
>>>
>>>will only read the first word (string of characters separated by whitespace)
>>>into the bufer pointed to by ptr.
>>>You can read a whole line with gets(). Unfortunately the function is flawed,
>>>because the buffer will overrun, probably causing a crash, if the user types
>>>in too many characters. fgets() is even worse if used incorrectly - input is
>>>silently truncated. Why is that worse? Because wrong results are usually
>>>worse than no results. However you can build a solid input function on top
>>>of fgets(), but it is needlessly complicated.
>>>So regrettably there is no easy way of getting a line from the user in ISO
>>>C.

>>
>> fgets() is worse than gets()?
>>
>> Nonsense.

>
> Well, in a way, it is: It is the closest you can get to a sensible
> text input function but falls short, unnecessarily and nastily.


OK, so it's not a perfect interface, lots of the C library isn't a
perfect interface. Whether it is 'necessary' to have had that interface
is a matter of opinion, but most of the I/O interface wasn't designed,
it "jest growed". It's certainly perfectly usable.

> If you consider a function more broken which does not invoke UB
> (and quite often segfaults to tell you where you went wrong)
> instead of silently truncating and upsetting everything in a
> hard-to-catch way, then you can arrive at this conclusion.


I have never had fgets() segfault, except when passing it invalid
parameters. How have you done it? If you give it a valid file pointer,
valid pointer to a data area and a correct length (no greater than the
size of the data area) then if it segfaults you have a faulty
implementation.

fgets() doesn't 'truncate', it simply stops inputting when the buffer is
full. Doing "if (strchr(buffer, '\n'))" will detect whether the line
was complete at that point (strlen() would do as well). You can then
decide whether you want to discard the rest of the data, increase the
buffer size, or whatever. If you don't bother testing then your program
is broken, no more or less than if you ignore returned status.

> IMO, fgets() with sensible return values to communicate, say
> EOF/Error/NoNewLineEncountered/NewLineEncountered, would be
> quite alright.


All that information is available. You can call feof/ferror on the
input stream (fgets() will return NULL if it didn't get anything) and
you can test for newline as above.

Or of course you can write your own input function with the behaviour
you like, it's not hard (indeed, I've rewritten such functions many
times because it was faster than digging out the code I'd written
before).

Chris C
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 16:36:11 +0000, Malcolm wrote:

>
> "Radith" <(E-Mail Removed)> wrote
>>
>> I use scanf("%s", &first) in order to get the input to the variable first
>> (which is char first[]).
>>

> scanf("%s", ptr);
>
> will only read the first word (string of characters separated by whitespace)
> into the bufer pointed to by ptr.
> You can read a whole line with gets(). Unfortunately the function is flawed,
> because the buffer will overrun, probably causing a crash, if the user types
> in too many characters.


Correct.

> fgets() is even worse if used incorrectly - input is
> silently truncated. Why is that worse? Because wrong results are usually
> worse than no results.


Often correct input has limited length. In which case fgets() (given a
suitable arguments) will only go "wrong" on invalid input, which is
perfectly reasonable.

Also you imply that gets() would give no results, which is wrong. fgets()
has well defined behaviour on too long input even if it isn't the
behaviour you want, gets() has undefined behaviour. Both can give results,
the difference is that with fgets() the results are well defined and quite
possibly recoverable, with gets() the whole program ceases to be
predictable. There is no sense in which fgets() is worse than gets().

> However you can build a solid input function on top
> of fgets(), but it is needlessly complicated.
> So regrettably there is no easy way of getting a line from the user in ISO
> C.


It is easy as long as you stipulate that valid lines must not exceed a
certain size. fgets() will still handle valid input fine and invalid input
in a controlled way.

Lawrence

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      08-01-2005

Michael Mair wrote:
> Keith Thompson wrote:
> > "Malcolm" <(E-Mail Removed)> writes:
> >
> >>"Radith" <(E-Mail Removed)> wrote
> >>
> >>>I use scanf("%s", &first) in order to get the input to the variable first
> >>>(which is char first[]).
> >>>
> >>
> >>scanf("%s", ptr);
> >>
> >>will only read the first word (string of characters separated by whitespace)
> >>into the bufer pointed to by ptr.
> >>You can read a whole line with gets(). Unfortunately the function is flawed,
> >>because the buffer will overrun, probably causing a crash, if the user types
> >>in too many characters. fgets() is even worse if used incorrectly - input is
> >>silently truncated. Why is that worse? Because wrong results are usually
> >>worse than no results. However you can build a solid input function on top
> >>of fgets(), but it is needlessly complicated.
> >>So regrettably there is no easy way of getting a line from the user in ISO
> >>C.

> >
> >
> > fgets() is worse than gets()?
> >
> > Nonsense.

>
> Well, in a way, it is: It is the closest you can get to a sensible
> text input function but falls short, unnecessarily and nastily.
> If you consider a function more broken which does not invoke UB
> (and quite often segfaults to tell you where you went wrong)
> instead of silently truncating and upsetting everything in a
> hard-to-catch way, then you can arrive at this conclusion.
> IMO, fgets() with sensible return values to communicate, say
> EOF/Error/NoNewLineEncountered/NewLineEncountered, would be
> quite alright.
>


Well, even though the function interface itself doesn't provide these
values, they can be tested for relatively easily:

if (fgets(buffer, sizeof buffer, stream))
{
if (strchr(buffer, '\n'))
{
/* NewLineEncountered */
}
else
{
/* NoNewLineEncountered */
}
}
else
{
if (feof(stream))
{
/* EOF */
}
else
{
/* Error */
}
}

I mean, The C Way is for the library to do ~90% of what you need it to,
and then fill in the rest by hand. No, I'm not bitter, really.

For all its flaws, fgets() has one chief advantage over gets() -- you
don't run the risk of crashing your program or opening a security hole
*every* time you call it.

>
> Cheers
> Michael
> --
> E-Mail: Mine is an /at/ gmx /dot/ de address.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
spaces in scanf format string stathisgotsis@hotmail.com C Programming 3 11-05-2006 10:59 PM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
string.scanf? JustSomeGuy C++ 5 06-06-2004 04:16 PM
scanf string in python lehrig Python 7 07-19-2003 03:14 AM



Advertisments