fputc() and fgetc() in C Programming
Hello Everyone, In this article, we will discuss about two important function fputc() and fgetc() in C Programming. These functions are used for writing and reading characters to and from files. Understanding how they work is essential for handling file input and output operations efficiently. Whether you are new to C or have some experience, this guide will help you to understand basics of fputc() and fgetc() in C effectively with examples. Let’s get started.
fputc() function in C
The fputc() function in C Programming is used to write a single character to a specified file. It takes two arguments: the character to be written and a pointer to the file stream to write the character to. The syntax of the fputc() function is:
int fputc(int character, FILE *stream);
The character argument is the character to be written to the file, and stream is the pointer to the file stream to write the character to. The function returns the character that was written on success and EOF on failure.
Here is an example code that uses fputc() to write a character to a file:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "w");
if (fp == NULL) {
printf("Error opening file.");
return 1;
}
fputc('a', fp);
fclose(fp);
return 0;
}
In this example, we first open the file “file.txt” in write mode using the fopen() function. We then check if the file was opened successfully. If it was, we use fputc() to write the character ‘a’ to the file. Finally, we close the file using the fclose() function. The output of this code is a file named “file.txt” with the character ‘a’ in it.
fgetc() function in C
The fgetc() function in C is used to read a single character from a specified file. It takes one argument: a pointer to the file stream to read the character from. The syntax of the fgetc() function is:
int fgetc(FILE *stream);
The stream argument is the pointer to the file stream to read the character from. The function returns the character that was read on success and EOF on failure.
Here is an example code that uses fgetc() to read a character from a file:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("Error opening file.");
return 1;
}
int c = fgetc(fp);
printf("The character read from the file is %c.\\n", c);
fclose(fp);
return 0;
}
In this example, we first open the file “file.txt” in read mode using the fopen() function. We then check if the file was opened successfully. If it was, we use fgetc() to read a character from the file and store it in the variable c. We then print out the character that was read using printf(). Finally, we close the file using the fclose() function.
The output of this code depends on the contents of the file “file.txt”. If the file contains the character ‘a’, the output will be:
The character read from the file is a.
here’s a simpler version of the code that uses fputc() and fgetc() with a daily life example:
#include <stdio.h>
int main() {
// Write to file using fputc()
FILE *fp;
fp = fopen("grocery.txt", "w");
if (fp == NULL) {
printf("Error opening file.");
return 1;
}
char groceryItem[20];
printf("Enter a grocery item: ");
scanf("%s", groceryItem);
fputc(groceryItem[0], fp); // Write first character of grocery item to file
fclose(fp);
// Read from file using fgetc()
fp = fopen("grocery.txt", "r");
if (fp == NULL) {
printf("Error opening file.");
return 1;
}
printf("First letter of grocery item: %c", fgetc(fp)); // Read first character from file and print to console
fclose(fp);
return 0;
}
In this code, we prompt the user to enter a grocery item using printf() and read it using scanf(). We then open the file “grocery.txt” in write mode using fopen() and write the first character of the grocery item to the file using fputc(). Finally, we close the file using fclose().
Next, we open the file “grocery.txt” in read mode using fopen() and read the first character of the grocery item from the file using fgetc(). We then print the first character to the console using printf(). Finally, we close the file using fclose().
This code demonstrates how to use fputc() and fgetc() to write and read characters to and from a file, respectively. In this case, we used it to write and read the first character of a grocery item.