Re: A safe scanf function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxx>
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux