C Programming
About Lesson
1: Opening and Closing Files

In C programming, file handling involves operations like opening files, reading from files, writing to files, and closing files. To work with files, you need to use file pointers and functions provided by the <stdio.h> library.

  • Opening a File:

    • Use fopen() function to open a file in different modes ("r", "w", "a", "r+", "w+", "a+").
     
    #include <stdio.h>

    FILE *filePtr;
    filePtr = fopen("example.txt", "w"); // Open file in write mode
    if (filePtr == NULL) {
    printf("Error opening file!n");
    exit(1);
    }

  • Closing a File:

    • Use fclose() function to close an opened file.
     
    fclose(filePtr); // Close the file
2: Reading and Writing to Files

You can perform reading and writing operations on files using functions like fscanf(), fprintf(), fgets(), fputs(), fread(), and fwrite().

  • Writing to a File:

    • Use fprintf() or fputs() to write data to a file.
     
    fprintf(filePtr, "Hello, World!n");
  • Reading from a File:

    • Use fscanf() or fgets() to read data from a file.
     
    char buffer[100];
    fgets(buffer, sizeof(buffer), filePtr);
    printf("Data read from file: %sn", buffer);
3: File Pointers and Positioning

File pointers (FILE *) keep track of the current position within a file during read/write operations. You can control the file pointer’s position using functions like fseek() and ftell().

  • Moving File Pointer:

    • Use fseek() to move the file pointer to a specific position within the file.
     
    fseek(filePtr, 0, SEEK_END); // Move to the end of the file
  • Getting Current File Pointer Position:

    • Use ftell() to get the current position of the file pointer.
     
    long position = ftell(filePtr); // Get current position in bytes
    printf("Current file position: %ldn", position);
4: Practice and Examples
  • Practice Exercise:
    • Write a program that reads input from the user and saves it to a file.
    • Implement a function that reads data from a file and displays it on the console.
    • Develop a program that copies the contents of one file to another file.
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *inputFile, *outputFile;
char buffer[100];

// Open file for writing
outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
printf("Error opening output file!n");
exit(1);
}

// Get input from user and write to file
printf("Enter text to write to file: ");
fgets(buffer, sizeof(buffer), stdin);
fprintf(outputFile, "%s", buffer);

// Close output file
fclose(outputFile);

// Open file for reading
inputFile = fopen("output.txt", "r");
if (inputFile == NULL) {
printf("Error opening input file!n");
exit(1);
}

// Read data from file and display on console
printf("Contents of output.txt:n");
while (fgets(buffer, sizeof(buffer), inputFile) != NULL) {
printf("%s", buffer);
}

// Close input file
fclose(inputFile);

return 0;
}

File handling is essential for working with external data storage in C programming. Practice using file operations (fopen(), fclose(), fprintf(), fscanf(), fgets(), fputs(), fread(), fwrite()) to perform common tasks like reading from and writing to files. Understand file pointers and their role in navigating through files efficiently. Experiment with different file handling scenarios to gain confidence in managing file I/O operations effectively in C. Understanding file handling principles is crucial for developing robust applications that interact with external data sources.