By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

on fgets and sscanf and their bugs

P: n/a
until a poor newbie can build a better function than sscanf and fgets
scanf("%s", string) is like gets(string)
Apr 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
RSoIsCaIrLiIoA said:
until a poor newbie can build a better function than sscanf and fgets
scanf("%s", string) is like gets(string)


That is, it's an extremely bad idea, compared to fgets and parsing it
yourself (or, if you're careful, using sscanf).

The fgets function itself is not as good as a customised input routine that
does precisely what the experienced and knowledgeable programmer requires
but, in the absence of such a routine, fgets is your best bet every time
(except, perhaps, on a few occasions where getc might be considered more
appropriate).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 20 '06 #2

P: n/a
On Thu, 20 Apr 2006 07:29:43 +0000, Richard Heathfield
<in*****@invalid.invalid> wrote:
RSoIsCaIrLiIoA said:
until a poor newbie can build a better function than sscanf and fgets
scanf("%s", string) is like gets(string)
That is, it's an extremely bad idea, compared to fgets and parsing it
yourself (or, if you're careful, using sscanf).


than if you use "%s" in sscanf is the same
no in [s]scanf is should not allow to use "%s" **never** only
"%<digits>s" should be allow
the same it should not allowed to use
sprinf(strig, fmt, ...);
but
s1prinf(strig, size_of_string, fmt, ...);

some time ago i read a book on OS bsd and internet protocol and i see
everywhere sprintf(buffer, "%s" ) sscanf(here, "%s") etc

The fgets function itself is not as good as a customised input routine that
does precisely what the experienced and knowledgeable programmer requires
but, in the absence of such a routine, fgets is your best bet every time
(except, perhaps, on a few occasions where getc might be considered more
appropriate).


this is Newfgets that is like fgets but

int Newfgets(char *buffer, int size, FILE* pf);

if a=Newfgets(buffer, size,pf);

if the old fgets return not NULL => Newfgets return the len of written
buffer in a
if the old fgets return NULL => Newfgets return the len of written
buffer in -a
Apr 25 '06 #3

P: n/a
RSoIsCaIrLiIoA said:
On Thu, 20 Apr 2006 07:29:43 +0000, Richard Heathfield
<in*****@invalid.invalid> wrote:
RSoIsCaIrLiIoA said:
until a poor newbie can build a better function than sscanf and fgets
scanf("%s", string) is like gets(string)
That is, it's an extremely bad idea, compared to fgets and parsing it
yourself (or, if you're careful, using sscanf).


than if you use "%s" in sscanf is the same


Well, no, because what you can do is capture the string using fgets, as I
suggested, in which case you /know/ how long the string is, so you can
allocate storage for that length + 1, and *then* use %s in sscanf with
perfect safety.
no in [s]scanf is should not allow to use "%s" **never** only
From what you say, it seems as if you don't know what you're talking about.
It is certainly true that you should never use unadorned %s in scanf, but
in sscanf there are ways to make it safe.

<snip>
this is Newfgets that is like fgets but

int Newfgets(char *buffer, int size, FILE* pf);

if a=Newfgets(buffer, size,pf);

if the old fgets return not NULL => Newfgets return the len of written
buffer in a
if the old fgets return NULL => Newfgets return the len of written
buffer in -a


That isn't even remotely what I meant, because it doesn't solve the problem
most people have with fgets.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 25 '06 #4

P: n/a
Richard Heathfield wrote:
RSoIsCaIrLiIoA said:
On Thu, 20 Apr 2006 07:29:43 +0000, Richard Heathfield
RSoIsCaIrLiIoA said: until a poor newbie can build a better function than sscanf
and fgets scanf("%s", string) is like gets(string)

That is, it's an extremely bad idea, compared to fgets and
parsing it yourself (or, if you're careful, using sscanf).


than if you use "%s" in sscanf is the same


Well, no, because what you can do is capture the string using
fgets, as I suggested, in which case you /know/ how long the
string is, so you can allocate storage for that length + 1, and
*then* use %s in sscanf with perfect safety.


Why are you feeding the troll?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 25 '06 #5

P: n/a
CBFalconer said:
Why are you feeding the troll?


I'm correcting misinformation. Your opinion of its source is noted, but it
seemed to me that the subject under discussion was worth discussing. When a
subject seems to me to be worth discussing, I reserve the right to discuss
it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 25 '06 #6

P: n/a
Richard Heathfield wrote:
CBFalconer said:
Why are you feeding the troll?


I'm correcting misinformation. Your opinion of its source is noted,
but it seemed to me that the subject under discussion was worth
discussing. When a subject seems to me to be worth discussing, I
reserve the right to discuss it.


Obviously your prerogative. However considering the record of this
particular troll I consider any response to be pointless.
Misinformation is at the heart of trolling.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 25 '06 #7

P: n/a
CBFalconer said:
However considering the record of this
particular troll I consider any response to be pointless.


To him? Sure. To others, though, perhaps not.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.