🎓 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
fgetc() function in C is a standard library function that reads the next character from the specified input stream and returns it as an unsigned char cast to an int. It is part of the C standard library (stdio.h) and is commonly used for reading characters from files or standard input.fgetc() Function Syntax
The syntax for the fgetc() function is as follows:
int fgetc(FILE *stream);
Parameters:
stream: A pointer to aFILEobject that specifies the input stream.
Returns:
- The function returns the next character from the input stream as an unsigned char cast to an int. If the end of the file is reached or an error occurs,
EOFis returned.
Examples
Reading Characters from a File
To demonstrate how to use fgetc() to read characters from a file, we will write a simple program.
Example
#include <stdio.h>
int main() {
FILE *file;
int ch;
// 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 and print characters until the end of the file
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
// Close the file
fclose(file);
return 0;
}
Output (assuming example.txt contains the text "Hello, World!"):
Hello, World!
Reading Characters from Standard Input
This example shows how to use fgetc() to read characters from the standard input.
Example
#include <stdio.h>
int main() {
int ch;
// Prompt the user for input
printf("Enter text (press Enter to finish): ");
// Read and print characters until a newline character is encountered
while ((ch = fgetc(stdin)) != '\n' && ch != EOF) {
putchar(ch);
}
// Print a newline for clarity
putchar('\n');
return 0;
}
Output (example user input "Hello, World!"):
Enter text (press Enter to finish): Hello, World!
Hello, World!
Real-World Use Case
Processing a Configuration File
In real-world applications, the fgetc() function can be used to process a configuration file character by character.
Example
#include <stdio.h>
int main() {
FILE *file;
int ch;
int line_number = 1;
// 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;
}
// Print the line number and read characters until the end of the file
printf("%d: ", line_number);
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
if (ch == '\n' && (ch = fgetc(file)) != EOF) {
line_number++;
printf("%d: ", line_number);
ungetc(ch, file);
}
}
// Close the file
fclose(file);
return 0;
}
Output (assuming config.txt contains multiple lines of configuration settings):
1: setting1=value1
2: setting2=value2
3: setting3=value3
Conclusion
The fgetc() function is an essential tool for reading characters from an input stream in C. It allows you to process input data character by character, making it useful for tasks such as reading files or handling user input.
By understanding and using this function, you can efficiently manage character-based input operations in your C programs. Always ensure to handle the return value properly to check for the end of the file or any errors.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment