fgets
raw OCR
Gets a string from a stream.
Defined in header <stdio.h>
Syntax
#include <stdio.h>
char *fgets(char *s, int n, FILE *stream);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ | ■ |
Remarks
fgets reads characters from stream into the string s. The function stops reading when it reads either n-1 characters or a newline character, whichever comes first, fgets retains the newline character at the end of s. A null byte is appended to s to mark the end of the string.
Return value
On success, fgets returns the string pointed to by s; it returns null on end-of-file or error.
See also
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE * Stream;
char string! "This is a test";
char msg[20]
/* open a file for update */
stream = fopen( "DUMMY.FIL", "w+");
/* write a string into the file */
fwrite(string, strlen(string) , 1, stream);
/* seek to the start of the file */
fseek(stream, 0, SEEK_SET);
/* read a string from the file */
fgets(msg, strlen(string) +1, stream);
/* display the string */
printf("%s", msg) ;
fclose(stream);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/stdio.h/fgets/
· short form /3.1/fgets/