C stdio fgets() Function
Example
Read a line from a file:
FILE *fptr;
// Open a file in read mode
fptr =
fopen("filename.txt", "r");
// Store the content of the file
char
myString[100];
// Read the content and store it inside myString
fgets(myString, 100, fptr);
// Print the file content
printf("%s",
myString);
// Close the file
fclose(fptr);
Try it Yourself »
Definition and Usage
The fgets()
function reads content from the file up to the next line break and writes it into a char
array. A \0
null terminating character is appended to the end of the content. The position indicator is moved to the next unread character in the file.
The fgets()
function is defined in the <stdio.h>
header file.
Syntax
fgets(char * destination, int size, FILE * fptr);
Parameter Values
Parameter | Description |
---|---|
destination | Required. A pointer to the array where the content will be written. |
size | Required. The size of the array being written to. The function will read at most size-1 characters from the file. |
fptr |
Required. A file pointer, usually created by the fopen() function.
|
Technical Details
Returns: | The same pointer that was provided by the destination parameter. |
---|