🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
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 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).
Comments
Post a Comment
Leave Comment