📁 File Handling
Programs lose their data once they stop running. File handling allows you to store data permanently on your hard drive.
Examples
1. Create and Write
Rating: Beginner | Difficulty: Easy
Create a new file named test.txt and write "Hello Files!" into it.
💡 Hint
Use fopen with "w" mode in C, ofstream in C++, or FileWriter in Java.
2. Read and Display
Rating: Beginner | Difficulty: Easy
Read the contents of test.txt and print them to the console.
💡 Hint
Use a loop to read until the end of the file (EOF).
3. Append Content
Rating: Beginner | Difficulty: Easy Open an existing file and add a new line at the end without deleting existing data.
💡 Hint
Use the "append" mode ("a" in C or FileWriter(file, true) in Java).
4. Character Count
Rating: Beginner | Difficulty: Easy Read a file and count how many characters it contains.
💡 Hint
Increment a counter for every character read from the file.
5. Line Count
Rating: Intermediate | Difficulty: Medium Count how many lines of text are in a file.
💡 Hint
Count the number of
newline characters (\n).
6. Copy File
Rating: Intermediate | Difficulty: Medium
Read data from source.txt and write it into a new file destination.txt.
💡 Hint
Read one character/line at a time and immediately write it to the second file.
7. Search for a Word
Rating: Intermediate | Difficulty: Medium Ask the user for a word and check if it exists inside a specific file.
💡 Hint
Read the file word by word and use string comparison.
8. Word Frequency
Rating: Advanced | Difficulty: Hard Find how many times a specific word appears in a text file.
💡 Hint
Similar to search, but keep a counter instead of stopping at the first match.
9. Replace Word
Rating: Advanced | Difficulty: Hard Search for "oldWord" in a file and replace every occurrence with "newWord".
💡 Hint
Read from the source file, modify the strings in memory, and write to a temporary file. Then rename the temp file.
10. Merge Two Files
Rating: Intermediate | Difficulty: Medium Read contents from two different files and combine them into a third file.
💡 Hint
Copy file 1, then copy file 2 into the target file.
11. Student Records (Binary)
Rating: Advanced | Difficulty: Medium Store student data (Name, Roll No, Marks) into a binary file.
💡 Hint
Use struct in C/C++ or Serializable objects in Java.
12. List Files in Directory
Rating: Pro | Difficulty: Hard Write a program that prints the names of all files in the current folder.
💡 Hint
Requires system-level libraries like dirent.h in C or java.io.File in Java.
13. Read CSV and Calculate
Rating: Advanced | Difficulty: Medium
Read a .csv file containing prices and calculate the total sum.
💡 Hint
Split each line by the comma , delimiter.
14. Reverse File Content
Rating: Pro | Difficulty: Hard Read a file and write its contents in reverse order into another file.
💡 Hint
Read everything into a large buffer/list first, then write it out backwards.
15. Simple Logger
Rating: Intermediate | Difficulty: Medium
Create a function logMessage(msg) that appends the current date/time and the message to a
log.txt file.
💡 Hint
Useful for debugging real applications!