The fgets() function in C is a standard library function that reads a line from the specified stream and stores it into the string pointed to by str. It is part of the C standard library (stdio.h) and is commonly used for reading input from a file or from the standard input (stdin).
Table of Contents
- Introduction
fgets()Function Syntax- Understanding
fgets() - Examples
- Reading a Line from a File
- Reading a Line from Standard Input
- Real-World Use Case
- Conclusion
Introduction
The fgets() function is used for reading a line of text from a stream and storing it into a buffer. It is commonly used for reading strings from files or user input.
fgets() Function Syntax
The syntax for the fgets() function is as follows:
char *fgets(char *str, int n, FILE *stream);
Parameters:
str: A pointer to an array of characters where the string read is stored.n: The maximum number of characters to be read (including the null terminator).stream: A pointer to aFILEobject that specifies an input stream.
Returns:
- The function returns
stron success, andNULLif an error occurs or end-of-file is reached while no characters have been read.
Understanding fgets()
The fgets() function reads characters from the specified stream until a newline character is encountered, n-1 characters are read, or end-of-file is reached, whichever comes first. The newline character, if encountered, is included in the string and a null character is appended to terminate the string.
Examples
Reading a Line from a File
To demonstrate how to use fgets() to read a line from a file, we will write a simple program.
Example
#include <stdio.h>
int main() {
FILE *file;
char buffer[100];
// Open the file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error: Could not open file for reading.\n");
return 1;
}
// Read a line from the file
if (fgets(buffer, sizeof(buffer), file) != NULL) {
// Print the read line
printf("Read line: %s", buffer);
} else {
printf("Error: Could not read from file.\n");
}
// Close the file
fclose(file);
return 0;
}
Output (assuming example.txt contains some text):
Read line: This is an example line from the file.
Reading a Line from Standard Input
This example shows how to use fgets() to read a line from standard input.
Example
#include <stdio.h>
int main() {
char buffer[100];
// Prompt the user for input
printf("Enter a line of text: ");
// Read a line from standard input
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
// Print the read line
printf("You entered: %s", buffer);
} else {
printf("Error: Could not read from standard input.\n");
}
return 0;
}
Output (example user input "Hello, World!"):
Enter a line of text: Hello, World!
You entered: Hello, World!
Real-World Use Case
Reading Configuration Lines from a File
In real-world applications, the fgets() function can be used to read lines from a configuration file.
Example
#include <stdio.h>
int main() {
FILE *file;
char buffer[100];
// Open the configuration file for reading
file = fopen("config.txt", "r");
if (file == NULL) {
printf("Error: Could not open configuration file for reading.\n");
return 1;
}
// Read lines from the file and process them
while (fgets(buffer, sizeof(buffer), file) != NULL) {
// Process the configuration line (here we simply print it)
printf("Config line: %s", buffer);
}
// Close the file
fclose(file);
return 0;
}
Output (assuming config.txt contains configuration lines):
Config line: url=http://example.com
Config line: timeout=5000
Config line: maxConnections=100
Conclusion
The fgets() function is essential for reading lines of text from a stream in C. It allows you to read a specified number of characters from a stream and store them in a buffer, ensuring that the string is properly null-terminated. By understanding and using this function, you can efficiently handle text input in your C programs. Always ensure to check the return value of fgets() to handle errors and end-of-file conditions correctly.