File Handling Fundamentals: Streams, Modes & Character/Line I/O
Unit 5: Searching, Sorting, Dynamic Memory & File Handling • Problem Solving Using C
👨🏫 Professor's Mental Model: The Secure Warehouse Filing Cabinet & Courier Conveyor
RAM ek aisi temporary table hai jisme program band hote hi sab kuch gayab ho jata hai (Volatile)! Agar data ko hamesha ke liye save rakhna hai toh use Hard Disk ki file me store karna padta hai (Non-Volatile). FILE pointer ek courier boy jaisa hai jo file ka darwaza kholta hai (fopen), data ko ek-ek karke stream karta hai, aur kaam khatam hote hi darwaza lock karke chabi lauta deta hai (fclose)! Agar lock nahi lagaya toh data corruption ho sakta hai!
Academic Lecture Notes & Solved Study Pages
Unit 5 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. The Need for File Persistence & The FILE Structure Pointer
2. Opening Files with fopen() & The 6 Primary Access Modes
Syntax: `FILE *fp = fopen("filename.txt", "mode");`
1. "r" (Read): Opens existing file for reading. Returns NULL if file does not exist.
2. "w" (Write): Creates a new empty file for writing. If file already exists, ITS CONTENTS ARE COMPLETELY TRUNCATED (ERASED)!
3. "a" (Append): Opens file for writing at the END. Preserves existing data. Creates new file if missing.
4. "r+" (Read + Write): Opens file for both reading and writing. File must exist.
5. "w+" (Read + Write): Creates new empty file for read/write. Erases existing content.
6. "a+" (Read + Append): Opens for read and append at end.
3. Closing Files with fclose() & Safe Error Checking
`if (fp == NULL) { printf("File could not be opened!"); exit(1); }`
Operating systems buffer file writes in RAM. Calling `fclose()` forcefully flushes buffered data to physical disk platter, releases OS file locks, and frees the file descriptor table entry.
4. Character and String File I/O Functions
| Mode | Operation Allowed | If File Already Exists | If File Does Not Exist | Initial File Pointer Position |
|---|---|---|---|---|
| "r" | Read Only | Opens normally | Fails: Returns NULL pointer | Beginning of file |
| "w" | Write Only | Completely OVERWRITES & erases content | Creates new empty file | Beginning of file |
| "a" | Append (Write) | Preserves existing data | Creates new file | End of file (EOF) |
| "r+" | Read & Write | Preserves existing data | Fails: Returns NULL pointer | Beginning of file |
| "w+" | Read & Write | Erases existing data completely | Creates new empty file | Beginning of file |
| "a+" | Read & Append | Preserves existing data | Creates new file | End of file for writes |
Interactive Tested Code Example
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
char filename[] = "sample_notes.txt";
// 1. Write formatted data to file
fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: Unable to create file.\n");
return 1;
}
fprintf(fp, "DevBCA University Portal\n");
fprintf(fp, "Course: Problem Solving Using C\n");
fprintf(fp, "Score: %d Marks\n", 100);
fclose(fp); // Flush and close file
// 2. Read contents back using fgetc until EOF
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: Unable to open file for reading.\n");
return 1;
}
printf("=== READING FILE STREAM CONTENTS ===\n");
int ch;
while ((ch = fgetc(fp)) != EOF) {
putchar(ch); // Echo character to terminal
}
fclose(fp);
return 0;
}🎯 University Exam Scoring Blueprint
- In university exams, always check if (fp == NULL) after every fopen call.
- Emphasize that fgetc() returns an 'int', NOT a char, because EOF is defined as integer -1 which exceeds 8-bit unsigned char.
- Remember that mode 'w' immediately destroys and truncates existing file contents.