fgets, fgetws
Remonter à Stdio.h - Index
Header File
stdio.h
Category
Input/output Routines
Prototype
char *fgets(char *s, int n, FILE *stream);
wchar_t *fgetws(wchar_t *s, int n, FILE *stream); // Unicode version
Description
Gets a string from a stream.
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.
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;
}
Portability
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
fgets |
+ |
+ |
+ |
+ |
fgetws |
+ |
+ |
+ |