A safe scanf function
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.
Signature · 3 lines
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
Quoted 3 lines · Shriramana Sharma
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).
Quoted 2 lines
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.
Signature · 1 line
Glynn Clements <[email protected]>
Sunday, 26 February 2006 21:04 samaye tvayaa likhitam:
Quoted 2 lines
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.
Signature · 3 lines
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
Quoted 6 lines · Shriramana Sharma
Quoted 2 lines
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.
Signature · 1 line
Glynn Clements <[email protected]>
Monday, 27 February 2006 21:16 samaye tvayaa likhitam:
Quoted 3 lines
And
scanf()isn't "unsafe"; it just doesn't do what you want in your particular application. There are plenty of situations wherescanf()does the right thing andgetline()+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.
Signature · 3 lines
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