C File Read/Write Program


In C, file handling is performed using functions provided by the standard library, allowing you to read from and write to files. This capability is crucial for data persistence, enabling your programs to store and retrieve information even after they terminate.

Program: File Read/Write in C

Here's a simple program that demonstrates how to write data to a file and then read it back.

#include <stdio.h> #include <stdlib.h> int main() { FILE *file; char data[100]; // Writing to a file file = fopen("example.txt", "w"); // Open file in write mode if (file == NULL) { perror("Error opening file for writing"); return EXIT_FAILURE; } // Prompting user for input printf("Enter some text to write to the file: "); fgets(data, sizeof(data), stdin); // Read a line of input from the user // Write data to file fprintf(file, "%s", data); fclose(file); // Close the file after writing // Reading from the file file = fopen("example.txt", "r"); // Open file in read mode if (file == NULL) { perror("Error opening file for reading"); return EXIT_FAILURE; } printf("Contents of the file:\n"); while (fgets(data, sizeof(data), file) != NULL) { printf("%s", data); // Print each line read from the file } fclose(file); // Close the file after reading return EXIT_SUCCESS; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header file is required for standard input/output functions like printf, fgets, and file handling functions.
    • #include <stdlib.h>: This header file is included for the EXIT_FAILURE and EXIT_SUCCESS macros.
  2. File Pointer:

    • FILE *file;: This declares a pointer to a FILE type, which is used to handle the file.
  3. Writing to a File:

    • Open File:
      file = fopen("example.txt", "w");
      This opens (or creates) a file named example.txt in write mode. If the file already exists, its contents will be erased.
    • Error Checking:
      if (file == NULL) { perror("Error opening file for writing"); return EXIT_FAILURE; }
      This checks if the file was successfully opened. If not, an error message is displayed.
    • User Input:
      fgets(data, sizeof(data), stdin);
      This reads a line of text from standard input into the data array.
    • Write to File:
      fprintf(file, "%s", data);
      This writes the contents of data to the file.
    • Close File:
      fclose(file);
      This closes the file after writing.
  4. Reading from a File:

    • Open File:
      file = fopen("example.txt", "r");
      This opens the file in read mode.
    • Error Checking: Similar to the write mode, it checks if the file opened successfully.
    • Read and Print:
      while (fgets(data, sizeof(data), file) != NULL) { printf("%s", data); }
      This reads lines from the file and prints them to the console until the end of the file is reached.
    • Close File: Again, the file is closed after reading.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code:

    gcc file_read_write.c -o file_read_write
  2. Execute the Program:

    ./file_read_write

Example Output

When you run the program, it will prompt you to enter some text, then it will write that text to a file and read it back:

Enter some text to write to the file: Hello, World! Contents of the file: Hello, World!

Conclusion

This program demonstrates basic file handling in C, including writing to and reading from a text file. Understanding file operations is essential for developing applications that require data persistence, such as saving user settings, logging events, or processing larger datasets. With these basic concepts, you can extend your file handling skills to include more complex operations like binary file reading/writing, file appending, and handling different file formats.