Why files are needed in C programming language?

Files are collections of records or storage locations on hard disk where data is stored permanently. In C programming, files allow programs to store and retrieve data that persists beyond program execution. We can access files using various C library functions for different operations.

Need of Files in C Programming

Files are essential in C programming for the following reasons −

  • Data Persistence: Entire data is lost when the program terminates. Storing data in files preserves your information even after the program ends.

  • Time Efficiency: If you need to enter large amounts of data, it normally takes considerable time to input everything manually each time.

  • Easy Data Access: With a file containing all the data, you can easily access the contents using a few C commands.

  • Data Portability: You can easily transfer your data from one computer to another without any modifications.

File Operations in C

The basic operations that can be performed on files in C are −

  • Creating/Naming the file
  • Opening the file
  • Reading from the file
  • Writing into the file
  • Closing the file

Syntax

The syntax for file pointer declaration and opening

FILE *file_pointer;
file_pointer = fopen("filename", "mode");

The syntax for reading from file

int fgetc(FILE *fp);           // read single character
int fscanf(FILE *fp, "format", variables);  // read formatted data

The syntax for writing to file

int fputc(int c, FILE *fp);    // write single character  
int fprintf(FILE *fp, "format", variables); // write formatted data

Example: File Write and Read Operations

Following is a C program demonstrating basic file operations −

#include <stdio.h>
#include <string.h>

int main() {
    FILE *femp;
    char empname[50];
    int empnum;
    float empsal;
    
    // Opening file for writing
    femp = fopen("employee.txt", "w");
    if (femp == NULL) {
        printf("Error opening file!<br>");
        return 1;
    }
    
    // Writing data to file
    strcpy(empname, "John Doe");
    empnum = 101;
    empsal = 50000.50;
    
    fprintf(femp, "%s<br>", empname);
    fprintf(femp, "%d<br>", empnum);
    fprintf(femp, "%.2f<br>", empsal);
    
    printf("Data written to file successfully.<br>");
    fclose(femp);
    
    // Opening file for reading
    femp = fopen("employee.txt", "r");
    if (femp == NULL) {
        printf("Error opening file for reading!<br>");
        return 1;
    }
    
    // Reading data from file
    fscanf(femp, "%s", empname);
    fscanf(femp, "%d", &ampnum);
    fscanf(femp, "%f", &ampsal);
    
    // Displaying read data
    printf("\nData read from file:<br>");
    printf("Employee name: %s<br>", empname);
    printf("Employee number: %d<br>", empnum);
    printf("Employee salary: %.2f<br>", empsal);
    
    fclose(femp);
    return 0;
}
Data written to file successfully.

Data read from file:
Employee name: John
Employee number: 101
Employee salary: 50000.50

Key Points

  • Always check if file opening was successful by verifying the file pointer is not NULL
  • Use appropriate file modes: "r" for reading, "w" for writing, "a" for appending
  • Always close files using fclose() to free system resources
  • File operations require proper error handling for robust programs

Conclusion

Files in C programming provide essential data persistence, allowing programs to store and retrieve information beyond program execution. Mastering file operations is crucial for developing practical C applications that handle real-world data storage requirements.

Updated on: 2026-03-15T13:10:20+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements