Node:Line Input, Next:Unreading, Previous:Character Input, Up:I/O on Streams
Since many programs interpret input on the basis of lines, it is convenient to have functions to read a line of text from a stream.
Standard C has functions to do this, but they aren't very safe: null
characters and even (for gets
) long lines can confuse them. So
the GNU library provides the nonstandard getline
function that
makes it easy to read lines reliably.
Another GNU extension, getdelim
, generalizes getline
. It
reads a delimited record, defined as everything through the next
occurrence of a specified delimiter character.
All these functions are declared in stdio.h
.
ssize_t getline (char **lineptr, size_t *n, FILE *stream) | Function |
This function reads an entire line from stream, storing the text
(including the newline and a terminating null character) in a buffer
and storing the buffer address in *lineptr .
Before calling If you set In either case, when When This function is a GNU extension, but it is the recommended way to read lines from a stream. The alternative standard functions are unreliable. If an error occurs or end of file is reached without any bytes read,
|
ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream) | Function |
This function is like getline except that the character which
tells it to stop reading is not necessarily newline. The argument
delimiter specifies the delimiter character; getdelim keeps
reading until it sees that character (or end of file).
The text is stored in lineptr, including the delimiter character
and a terminating null. Like
ssize_t getline (char **lineptr, size_t *n, FILE *stream) { return getdelim (lineptr, n, '\n', stream); } |
char * fgets (char *s, int count, FILE *stream) | Function |
The fgets function reads characters from the stream stream
up to and including a newline character and stores them in the string
s, adding a null character to mark the end of the string. You
must supply count characters worth of space in s, but the
number of characters read is at most count - 1. The extra
character space is used to hold the null character at the end of the
string.
If the system is already at end of file when you call Warning: If the input data has a null character, you can't tell.
So don't use |
wchar_t * fgetws (wchar_t *ws, int count, FILE *stream) | Function |
The fgetws function reads wide characters from the stream
stream up to and including a newline character and stores them in
the string ws, adding a null wide character to mark the end of the
string. You must supply count wide characters worth of space in
ws, but the number of characters read is at most count
- 1. The extra character space is used to hold the null wide
character at the end of the string.
If the system is already at end of file when you call Warning: If the input data has a null wide character (which are
null bytes in the input stream), you can't tell. So don't use
|
char * fgets_unlocked (char *s, int count, FILE *stream) | Function |
The fgets_unlocked function is equivalent to the fgets
function except that it does not implicitly lock the stream.
This function is a GNU extension. |
wchar_t * fgetws_unlocked (wchar_t *ws, int count, FILE *stream) | Function |
The fgetws_unlocked function is equivalent to the fgetws
function except that it does not implicitly lock the stream.
This function is a GNU extension. |
char * gets (char *s) | Deprecated function |
The function gets reads characters from the stream stdin
up to the next newline character, and stores them in the string s.
The newline character is discarded (note that this differs from the
behavior of fgets , which copies the newline character into the
string). If gets encounters a read error or end-of-file, it
returns a null pointer; otherwise it returns s.
Warning: The |