C Program to Write a Sentence to a File

 Writing to a file in C involves several steps:

  1. Include necessary header files: To work with files in C, you need to include the stdio.h header file, which provides functions like fopen, fwrite, and fclose for file operations.

  2. Declare a FILE pointer: You need to declare a pointer to a FILE structure, which will be used to represent the file you want to work with.

  3. Open the file: You need to use the fopen function to open a file. This function takes two arguments: the name of the file you want to open and the mode in which you want to open it. For writing, you should use the "w" mode, which creates a new file or overwrites an existing file. If the file doesn't exist, it will be created; if it does exist, its contents will be truncated.

  4. Write to the file: You can use functions like fprintf or fputs to write data to the file. In this case, we will use fprintf to write a sentence to the file.

  5. Close the file: After you're done with writing, you should close the file using the fclose function. This step is important to ensure that any pending writes are flushed to the file and to release system resources associated with the file.

Here's a C program that writes a sentence to a file and then reads and displays the content of the file:

#include <stdio.h>

int main() {
    // Declare a FILE pointer
    FILE *file;

    // Open the file for writing
    file = fopen("output.txt", "w");

    // Check if the file was opened successfully
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1; // Exit the program with an error code
    }

    // Write a sentence to the file using fprintf
    fprintf(file, "This is a sentence written to a file.");

    // Close the file
    fclose(file);

    // Reopen the file for reading
    file = fopen("output.txt", "r");

    // Check if the file was opened successfully
    if (file == NULL) {
        printf("Failed to open the file for reading.\n");
        return 1;
    }

    // Read and display the content of the file
    char ch;
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }

    // Close the file again
    fclose(file);

    return 0; // Exit the program with a success code

Explanation:

  1. The program opens a file called "output.txt" in write mode and writes the sentence "This is a sentence written to a file." to it.
  2. Then, it closes the file to ensure that the data is flushed to the file.
  3. It reopens the same file in read mode and reads and displays its content character by character until the end of the file (EOF) is reached.
  4. Finally, it closes the file again.

Output:

This is a sentence written to a file.

This program creates and writes to the "output.txt" file and then reads and displays the content from the same file.

Prasun Barua

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other published books are Solar PV System Design and Technology, Electricity from Renewable Energy, Tech Know Solar PV System, C Coding Practice, AI and Robotics Overview, Robotics and Artificial Intelligence, Know How Solar PV System, Know The Product, Solar PV Technology Overview, Home Appliances Overview, Tech Know Solar PV System, C Programming Practice, etc. These books are available at Google Books, Google Play, Amazon and other platforms.

*

Post a Comment (0)
Previous Post Next Post