Introduction to C fputc() and fgetc() Functions
In the C programming language, the fputc() and fgetc() functions are used to write and read characters to and from a file, respectively. These functions are part of the standard input/output library in C and are commonly used for file handling operations.
The fputc() Function
The fputc() function is used to write a single character to a file. It takes two arguments: the character to be written and the file pointer that represents the file to which the character will be written.
Here is the syntax for the fputc() function:
int fputc(int character, FILE *file_pointer);
The character argument is the ASCII value of the character to be written. The file_pointer argument is a pointer to a FILE structure that represents the file. The function returns the character written as an unsigned char cast to an int, or EOF if an error occurs.
Here is an example that demonstrates the usage of the fputc() function:
#include <stdio.h> int main() { FILE *file_pointer; int character = 'A'; file_pointer = fopen("example.txt", "w"); if (file_pointer == NULL) { printf("Error opening file."); return 1; } fputc(character, file_pointer); fclose(file_pointer); return 0; }
In this example, the fputc() function is used to write the character ‘A’ to a file named “example.txt”. The file is opened in write mode (“w”) using the fopen() function, and if the file pointer is NULL (indicating an error in opening the file), an error message is printed. After writing the character, the file is closed using the fclose() function.
The fgetc() Function
The fgetc() function is used to read a single character from a file. It takes one argument: the file pointer that represents the file from which the character will be read.
Here is the syntax for the fgetc() function:
int fgetc(FILE *file_pointer);
The file_pointer argument is a pointer to a FILE structure that represents the file. The function returns the character read as an unsigned char cast to an int, or EOF if the end of the file is reached or an error occurs.
Here is an example that demonstrates the usage of the fgetc() function:
#include <stdio.h> int main() { FILE *file_pointer; int character; file_pointer = fopen("example.txt", "r"); if (file_pointer == NULL) { printf("Error opening file."); return 1; } character = fgetc(file_pointer); printf("The character read is: %cn", character); fclose(file_pointer); return 0; }
In this example, the fgetc() function is used to read a character from a file named “example.txt”. The file is opened in read mode (“r”) using the fopen() function, and if the file pointer is NULL (indicating an error in opening the file), an error message is printed. The character read is then printed using the printf() function, and the file is closed using the fclose() function.
The fputc() and fgetc() functions are essential for performing file handling operations in C. They provide a convenient way to write and read characters to and from files, respectively. By understanding how these functions work, you can effectively manipulate files and handle data in your C programs.