C fputc() and fgetc()

The functions fputc() and fgetc() are part of the C standard library’s file input/output (I/O) operations for handling individual characters in files. They are typically used for reading and writing character data to and from files.

  1. fputc() function: The fputc() function is used to write a single character to a file. Its syntax is as follows:
int fputc(int character, FILE *stream);
  • character: This is the character to be written to the file. It is passed as an int argument, but only the lowest 8 bits are used for the character value.
  • stream: This is a pointer to the FILE object that represents the file to which the character is written.

The function returns the character written as an unsigned char cast to an int, or EOF if an error occurs.

Example usage:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file != NULL) {
        int result = fputc('A', file);
        if (result != EOF) {
            printf("Character written successfully!\n");
        } else {
            printf("Error occurred while writing character.\n");
        }
        fclose(file);
    }
    return 0;
}

2. fgetc() function: The fgetc() function is used to read a single character from a file. Its syntax is as follows:

int fgetc(FILE *stream);
  • stream: This is a pointer to the FILE object that represents the file from which the character is read.

The function returns the character read as an unsigned char cast to an int, or EOF if the end of the file or an error occurs.

Example usage:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file != NULL) {
        int character = fgetc(file);
        if (character != EOF) {
            printf("Read character: %c\n", character);
        } else {
            printf("Error occurred while reading character.\n");
        }
        fclose(file);
    }
    return 0;
}

Remember to include the <stdio.h> header file at the beginning of your C program to use these functions. Also, ensure that you have appropriate file permissions and error handling in your code.

Writing File : fputc() function

Certainly! Here’s an example that demonstrates the usage of the fputc() function to write a string to a file character by character:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file != NULL) {
        const char *text = "Hello, World!";
        int i = 0;
        while (text[i] != '\0') {
            int result = fputc(text[i], file);
            if (result == EOF) {
                printf("Error occurred while writing character.\n");
                break;
            }
            i++;
        }
        fclose(file);
    } else {
        printf("Failed to open the file.\n");
    }
    return 0;
}

In this example, we open a file named “example.txt” in write mode using fopen(). We then check if the file was successfully opened. If it was, we define a string text containing the message we want to write to the file.

Next, we use a while loop to iterate through each character of the string text until we reach the null character '\0', which indicates the end of the string. Inside the loop, we call fputc() to write each character to the file. If fputc() returns EOF, we print an error message and break out of the loop.

Finally, we close the file using fclose().

After running this program, you should find that the file “example.txt” is created in the same directory as your C program, and it contains the string “Hello, World!” written character by character.

Reading File : fgetc() function

Certainly! Here’s an example that demonstrates the usage of the fgetc() function to read a file character by character:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file != NULL) {
        int character;
        while ((character = fgetc(file)) != EOF) {
            printf("%c", character);
        }
        fclose(file);
    } else {
        printf("Failed to open the file.\n");
    }
    return 0;
}

In this example, we open a file named “example.txt” in read mode using fopen(). We then check if the file was successfully opened. If it was, we define an integer variable character to store each character read from the file.

Next, we use a while loop to continuously call fgetc() to read characters from the file until the end of the file (EOF) is encountered. Inside the loop, we print each character using printf().

Finally, we close the file using fclose().

When you run this program, it will read the contents of the “example.txt” file character by character and print them to the console.