* A safe scanf function
@ 2006-02-26 8:17 Shriramana Sharma
2006-02-26 15:34 ` Glynn Clements
0 siblings, 1 reply; 5+ messages in thread
From: Shriramana Sharma @ 2006-02-26 8:17 UTC (permalink / raw)
To: Linux C Programming List
If I am not wrong, a safe alternate for scanf could be (relatively) easily
written which simply uses getline and sscanf -- thereby replacing the
original scanf function.
Why has not anyone written such a function and included it into the GNU C
library? Myself, I may write such a function for a specific case in my
program, but to modify the original library function I lack the know-how.
I request one of the guru-s here to do this most helpful task.
Thank you.
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-02-26 W08-7 UTC+0530
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: A safe scanf function
2006-02-26 8:17 A safe scanf function Shriramana Sharma
@ 2006-02-26 15:34 ` Glynn Clements
2006-02-27 15:15 ` Shriramana Sharma
0 siblings, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2006-02-26 15:34 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> If I am not wrong, a safe alternate for scanf could be (relatively) easily
> written which simply uses getline and sscanf -- thereby replacing the
> original scanf function.
You can't /replace/ scanf() in the C library. It's a standard ANSI
function, so it needs to be present and it needs to have its specified
behaviour (which can include consuming either more or less than a line
of input, depending on the format string).
> Why has not anyone written such a function and included it into the GNU C
> library?
Probably because its trivial to write it yourself if that's what you
really want.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: A safe scanf function
2006-02-26 15:34 ` Glynn Clements
@ 2006-02-27 15:15 ` Shriramana Sharma
2006-02-27 15:46 ` Glynn Clements
0 siblings, 1 reply; 5+ messages in thread
From: Shriramana Sharma @ 2006-02-27 15:15 UTC (permalink / raw)
To: Linux C Programming List
Sunday, 26 February 2006 21:04 samaye tvayaa likhitam:
> Probably because its trivial to write it yourself if that's what you
> really want.
But instead of forcing every author of a C program to implement such a
function, I still feel a safe version should be included in the library
itself.
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-02-27 W09-1 UTC+0530
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: A safe scanf function
2006-02-27 15:15 ` Shriramana Sharma
@ 2006-02-27 15:46 ` Glynn Clements
2006-02-28 4:29 ` Shriramana Sharma
0 siblings, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2006-02-27 15:46 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> > Probably because its trivial to write it yourself if that's what you
> > really want.
>
> But instead of forcing every author of a C program to implement such a
> function, I still feel a safe version should be included in the library
> itself.
Not every author of a C program needs such a function.
And scanf() isn't "unsafe"; it just doesn't do what you want in your
particular application. There are plenty of situations where scanf()
does the right thing and getline() + sscanf() doesn't; e.g.:
1. If you want to allow the data to be split over multiple lines.
scanf() treats all whitespace as equivalent, so you can use a newline
wherever you can use a space. This means that a data file will still
work if it has been wrapped, e.g. due to being sent via email.
2. If you don't want to parse a whole record in one step. You can read
the initial part of the data with one call to scanf() then, depending
upon that part, use different scanf() calls to read the rest of it.
You may be using "one line equals one record" for your data format,
but not everyone does. The main problem with that approach is that
your records can never be any larger than the maximum line length of
any program or communication channel through which the data has to
pass. Using a non-whitespace delimiter for records is safer.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: A safe scanf function
2006-02-27 15:46 ` Glynn Clements
@ 2006-02-28 4:29 ` Shriramana Sharma
0 siblings, 0 replies; 5+ messages in thread
From: Shriramana Sharma @ 2006-02-28 4:29 UTC (permalink / raw)
To: Linux C Programming List
Monday, 27 February 2006 21:16 samaye tvayaa likhitam:
> And scanf() isn't "unsafe"; it just doesn't do what you want in your
> particular application. There are plenty of situations where scanf()
> does the right thing and getline() + sscanf() doesn't; e.g.:
OK, so scanf does have its own applications.
I would like to place on record that I used fgets and sscanf and the problem I
was experiencing was totally removed.
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-02-28 W09-2 UTC+0530
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2006-02-28 4:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-02-26 8:17 A safe scanf function Shriramana Sharma
2006-02-26 15:34 ` Glynn Clements
2006-02-27 15:15 ` Shriramana Sharma
2006-02-27 15:46 ` Glynn Clements
2006-02-28 4:29 ` Shriramana Sharma
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).