File I/O
In C programming, File I/O (Input/Output) refers to reading from and writing to files stored on secondary storage (like hard disks), rather than working only with console (keyboard/screen).
Why is this important:
Section titled “Why is this important:”- Data persists even after the program ends.
- Allows handling large data.
- Used in real-world applications like databases, log systems, and configuration files.
File Handling in C
Section titled “File Handling in C”C uses a file pointer (FILE *) to manage files. All file operations are performed through this pointer.
The header file:
#include <stdio.h>
is required for file operations.
Key Steps in File I/O:
Section titled “Key Steps in File I/O:”Open a file (fopen) -> Perform operations (read/write/append) -> Close the file (fclose) -> File Modes in C
When opening a file using fopen(filename, mode), the mode defines what you can do.
Mode | Meaning |
---|---|
"r" | Read (file must exist) |
"w" | Write (create new or overwrite existing) |
"a" | Append (write at end of file) |
"r+" | Read + Write (file must exist) |
"w+" | Read + Write (create new or overwrite) |
"a+" | Read + Append |
- For binary files, add “b” → e.g., “rb”, “wb”, “ab”.
Opening a File
Section titled “Opening a File”FILE *fp;fp = fopen("data.txt", "r"); // open for reading
if(fp == NULL) { printf("Error opening file!\n"); return 1;}
- Always check if file opened successfully.
Closing a File
Section titled “Closing a File”fclose(fp);
- Frees resources and ensures data is written properly.
Writing to a File
Section titled “Writing to a File”Using fprintf()
Section titled “Using fprintf()”FILE *fp = fopen("data.txt", "w");fprintf(fp, "Hello, World!\n");fclose(fp);
Using fputs()
Section titled “Using fputs()”fputs("Hello, File Handling!", fp);
Using fputc()
Section titled “Using fputc()”fputc('A', fp);
Reading from a File
Section titled “Reading from a File”Using fscanf()
Section titled “Using fscanf()”FILE *fp = fopen("data.txt", "r");char str[50];fscanf(fp, "%s", str);
- Problem: Stops at whitespace.
Using fgets()
Section titled “Using fgets()”fgets(str, sizeof(str), fp);
Using fgetc()
Section titled “Using fgetc()”char ch = fgetc(fp);
Using fread() and fwrite() (Binary Files)
Section titled “Using fread() and fwrite() (Binary Files)”fread(buffer, size, count, fp);fwrite(buffer, size, count, fp);
- Used for structures, images, and other non-text data.
File Positioning Functions
Section titled “File Positioning Functions”Function | Purpose |
---|---|
ftell(fp) | Returns current position in file |
fseek(fp, offset, origin) | Move file pointer |
rewind(fp) | Reset pointer to beginning |
Example:
fseek(fp, 0, SEEK_END);long size = ftell(fp);rewind(fp);
Detecting End of File (EOF)
Section titled “Detecting End of File (EOF)”while(!feof(fp)) { char ch = fgetc(fp); if(ch != EOF) putchar(ch);}
- Better approach: Check return value of fgetc() instead of feof().
Error Handling in File I/O
Section titled “Error Handling in File I/O”- Use perror() or strerror(errno) for descriptive errors.
if(fp == NULL) { perror("File opening failed");}