PSUC Lab 13 — Persistent File Storage
Active Logic: File Streams in C (FILE *fp)
Master file streams in C: file pointers (FILE *fp), opening modes ("r", "w", "a"), character I/O (fgetc, fputc), line counting, file copying, file merging, and file statistics.
File Modes & Functions
| Mode / Function | Syntax / Rule | Key Trap / Pitfall |
|---|---|---|
| FILE *fp | fp = fopen("file.txt", "r"); | Always check if (fp == NULL) before reading/writing! |
| fgetc(fp) | Reads 1 char, returns EOF at end | Store return value in int ch to handle EOF cleanly! |
| fputc(ch, fp) | Writes 1 char to file stream | Always call fclose(fp) to flush write buffers! |
| fclose(fp) | Closes file stream | Forgetting fclose() causes data loss! |
🎯 Learning Philosophy: Active Logic Building
Test your file handling logic across 3 Interactive Learning Modes:
- 🧠 Tab 1 (Logic & Hints): File streams & NULL checks.
- 🧩 Tab 2 (Logic Puzzle): Complete missing file functions/modes & test virtual file simulators!
- 💻 Tab 3 (C Solution): Reference verified C code solutions.
Solve file handling puzzles before peeking at C reference code!
Exercise 1 of 4
Read File & Count Line Breaks
Opens file in "r" mode, reads character by character until EOF, and counts newline '\n' breaks.
File Stream Read Loop
- Open:
fp = fopen("sample.txt", "r"); - Read Loop:
while ((ch = fgetc(fp)) != EOF) { if (ch == '\n') lines++; } - Close:
fclose(fp);
Interactive Logic Hints
💡 Hint 1: What is EOF? ▼
EOF is a special constant (usually -1) returned by fgetc() when the end of file is reached!🧩 Complete File Reader Loop
FILE *fp = ("sample.txt", );
while ((ch = (fp)) != EOF) {
if (ch == '\n') lines++;
}
fclose(fp);
while ((ch = (fp)) != EOF) {
if (ch == '\n') lines++;
}
fclose(fp);
Select statements above and click Check!
📁 Virtual File Reader Simulator
Line count result...
read_file.cReference C Code
#include <stdio.h>
int main() {
FILE *fp = fopen("sample.txt", "r");
if (fp == NULL) { printf("Error opening file!\n"); return 1; }
int ch, lines = 0;
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') lines++;
}
fclose(fp);
printf("Total Lines = %d\n", lines + 1);
return 0;
}
Exercise 2 of 4
Copy File Contents (source.txt → dest.txt)
Reads from source.txt in "r" mode and writes to dest.txt in "w" mode using fputc().
File Copy Streams
- Source stream:
fsrc = fopen("source.txt", "r"); - Destination stream:
fdest = fopen("dest.txt", "w"); - Copy loop:
while ((ch = fgetc(fsrc)) != EOF) fputc(ch, fdest);
Interactive Logic Hints
💡 Hint 1: What happens if dest.txt already exists? ▼
Opening in
"w" mode OVERWRITES and TRUNCATES existing content in dest.txt!🧩 Complete File Copy Loop
FILE *fsrc = fopen("source.txt", "r");
FILE *fdest = fopen("dest.txt", );
while ((ch = fgetc(fsrc)) != EOF) {
(ch, fdest);
}
fclose(fsrc); fclose(fdest);
FILE *fdest = fopen("dest.txt", );
while ((ch = fgetc(fsrc)) != EOF) {
(ch, fdest);
}
fclose(fsrc); fclose(fdest);
Select statements above and click Check!
📋 Virtual File Copy Simulator
dest.txt output...
copy_file.cReference C Code
#include <stdio.h>
int main() {
FILE *fsrc = fopen("source.txt", "r");
FILE *fdest = fopen("dest.txt", "w");
if (fsrc == NULL || fdest == NULL) { printf("Error opening files!\n"); return 1; }
int ch;
while ((ch = fgetc(fsrc)) != EOF) {
fputc(ch, fdest);
}
fclose(fsrc); fclose(fdest);
printf("File copied successfully to dest.txt!\n");
return 0;
}
Exercise 3 of 4
Merge Two Files into a Third File
Appends contents of file1.txt and file2.txt into merged.txt.
File Merging Sequential Pass
- Pass 1: Copy
file1.txttomerged.txt - Pass 2: Copy
file2.txttomerged.txt
Interactive Logic Hints
💡 Hint 1: What mode to use for merged.txt in Pass 2? ▼
Use
"a" (append mode) for Pass 2 so it doesn't erase Pass 1 content!🧩 Complete Merge Mode Choice
// Pass 1: Create merged file
FILE *fout = fopen("merged.txt", );
copyStream(f1, fout);
fclose(fout);
// Pass 2: Append second file
fout = fopen("merged.txt", );
copyStream(f2, fout);
FILE *fout = fopen("merged.txt", );
copyStream(f1, fout);
fclose(fout);
// Pass 2: Append second file
fout = fopen("merged.txt", );
copyStream(f2, fout);
Select statements above and click Check!
🔀 Virtual File Merge Engine
merged.txt output...
merge_files.cReference C Code
#include <stdio.h>
int main() {
FILE *f1 = fopen("file1.txt", "r");
FILE *f2 = fopen("file2.txt", "r");
FILE *fout = fopen("merged.txt", "w");
int ch;
while ((ch = fgetc(f1)) != EOF) fputc(ch, fout);
while ((ch = fgetc(f2)) != EOF) fputc(ch, fout);
fclose(f1); fclose(f2); fclose(fout);
printf("Files merged successfully into merged.txt!\n");
return 0;
}
Exercise 4 of 4
File Character Statistics (Vowels, Digits, Spaces)
Reads file stream and computes counts of vowels, consonants, digits, and whitespace spaces.
Stream Classification Filters
- Vowels:
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') vowels++; - Digits:
if (isdigit(ch)) digits++; - Spaces:
if (ch == ' ' || ch == '\t') spaces++;
Interactive Logic Hints
💡 Hint 1: What functions in help? ▼
isalpha(ch), isdigit(ch), isspace(ch)!🧩 Complete File Stat Character Loop
while ((ch = fgetc(fp)) != ) {
char lower = tolower(ch);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') vowels++;
else if (isalpha(lower)) consonants++;
else if ((lower)) digits++;
}
char lower = tolower(ch);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') vowels++;
else if (isalpha(lower)) consonants++;
else if ((lower)) digits++;
}
Select statements above and click Check!
📊 File Content Statistics Engine
Character breakdown...
file_stats.cReference C Code
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *fp = fopen("sample.txt", "r");
if (fp == NULL) return 1;
int ch, v = 0, c = 0, d = 0, s = 0;
while ((ch = fgetc(fp)) != EOF) {
char l = tolower(ch);
if (l=='a'||l=='e'||l=='i'||l=='o'||l=='u') v++;
else if (isalpha(l)) c++;
else if (isdigit(l)) d++;
else if (isspace(l)) s++;
}
fclose(fp);
printf("Vowels: %d, Consonants: %d, Digits: %d, Spaces: %d\n", v, c, d, s);
return 0;
}
Knowledge Check
Test Your File Handling Mastery
Question 1 of 5
Loading question...