473,383 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

on fgets and sscanf and their bugs

until a poor newbie can build a better function than sscanf and fgets
scanf("%s", string) is like gets(string)
Apr 20 '06 #1
7 2464
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eirik | last post by:
Hey, all groovy C programmers, I've read in the FAQ(question 12.18) about how applications skip calls to (f)gets() after scanf() has been used. How can I avoid this? I know that I can by putting...
3
by: Tcc | last post by:
Hi all, Assume there are some data in a file "a.txt": abc def 11<---------------------data in a.txt is it possible for me to use "fgets" function to get the string "abc", "def" and "11"...
6
by: AMT2K5 | last post by:
Hello. I have a file (for a school assignment) with the following format and delimiter format. Each record in the file has the following format: 123423454567987,29873,James,Harry,St....
7
by: Michael R. Copeland | last post by:
I'm processing a control file comprised of many types of lines, with some containing variable data. I have a problem parsing the following data: 18 12.2 7.145 6.214 Phase distances First,...
1
by: anonymous | last post by:
I'm having a LOT of trouble figuring out how to read input from a file into my program. This file contains lines with an arbitrary number of words in them. For example: This is one line in my file...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
6
by: DrSchwartz | last post by:
Hi all, I'm completely stuck.. I have a program which reads integers (there are 65536 integers) from one file, and adds them into the Bloom filter. And then it reads another file (with a larger set...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.