String
Input/Output
Because strings are recognized to be
special objects in C, some special library functions for reading and writing
are provided for them. These make it easier to deal with strings, without the
need for special user-routines. There are four of these functions:
gets()
puts()
sprintf()
sscanf()
gets()
This function fetches a string from
the standard input file stdin and places it into some buffer which the
programmer must provide.
#define
SIZE 255
char
*sptr, buffer[SIZE];
strptr
= gets(buffer);
If the routine is successful in
getting a string, it returns the value buffer to the string pointer strptr.
Otherwise it returns NULL (==0). The advantage of gets() over scanf("%s"..)
is that it will read spaces in strings, whereas scanf() usually
will not. gets() quits reading when it finds a newline character: that is,
when the user presses RETURN.
NOTE: there are valid concerns about
using this function. Often it is implemented as a macro with poor bounds
checking and can be exploited to produce memory corruption by system attackers.
In order to write more secure code, use fgets() instead.
puts()
puts() sends a string to the output file stdout, until it finds a NULL
end of string marker. The NULL byte is not written to stdout, instead a newline character
is written.
char
*string;
int
returncode;
returncode
= puts(string);
puts() returns an integer value, whose value is only guaranteed if
there is an error. returncode
== EOF if an end of file was encountered
or there was an error.
sprintf()
This is an interesting function
which works in almost the same way as printf(), the
exception being that it prints to a string! In other words it treats a string
as though it were an output file. This is useful for creating formatted strings
in the memory. On most systems it works in the following way:
int
n;
char
*sp;
n
= sprintf (sp, "control string", parameters, values);
n is an integer which is the number of characters printed. sp
is a pointer to the destination string or the string which is to be written to.
Note carefully that this function does not perform any check on the output
string to make sure that it is long enough to contain the formatted output. If
the string is not large enough, then a crash could be in store! This can also
be considered a potential security problem, since buffer overflows can be used
to capture control of important programs. Note that on system V Unix systems
the sprintf functionr returns a pointer to the start of the printed
string, breaking the pattern of the other printf functions. To make such an
implementation compatible with the usual form you would have to write:
n = strlen(sprintf(parameters......));
sscanf()
This function is the complement of sprintf(). It reads its input from a string, as though it were an
input file.
int
n;
char
*sp;
n
= sscanf (sp,"control string", pointers...);
sp is a pointer to the string which is to be read from. The
string must be NULL terminated (it must have a zero-byte end marker '\0'). sscanf() returns an integer value which holds the number of items
successfully matched or EOF if an end of file marker was read or an error occurred. The
conversion specifiers are identical to those for scanf().
0 comments:
Post a Comment