on fgets and sscanf and their bugs

R

RSoIsCaIrLiIoA

until a poor newbie can build a better function than sscanf and fgets
scanf("%s", string) is like gets(string)
 
R

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).

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).
 
R

RSoIsCaIrLiIoA

RSoIsCaIrLiIoA said:

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 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
 
R

Richard Heathfield

RSoIsCaIrLiIoA said:
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 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.

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.
 
C

CBFalconer

Richard said:
RSoIsCaIrLiIoA said:

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/>
 
R

Richard Heathfield

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.
 
C

CBFalconer

Richard said:
CBFalconer said:


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/>
 
R

Richard Heathfield

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top