Practice Top 30 C++ Programming Questions on File Handling, TechnoVlogs

Practice Top 30 C++ Programming Questions on File Handling


Q1. Write a program to create a file and write the text "Hello, World!" into it.

Input:  
None  

Expected Output:  
File created and written successfully.  
Content of the file:  

Hello, World!

Q2. Write a program to read the content of a file named example.txt.

Input:  
Content of example.txt:  
This is a sample file.

Expected Output:  
File Content:
This is a sample file.

Q3. Write a program to check whether a file named test.txt exists.

Input:  
File: test.txt exists.  

Expected Output:  
File exists.

Q4. Write a program to append the text "Appended Line" to an existing file.

Input:  
Content of data.txt:  
First Line

Expected Output:  
Text appended successfully.
Updated file content:  
First Line
Appended Line

Q5. Write a program to count the number of words in a file.

Input:  
Content of words.txt:  
C++ is amazing.

Expected Output:  
Word Count: 3

Q6. Write a program to count the number of lines in a file.

Input:  
Content of lines.txt:  
Line 1
Line 2
Line 3

Expected Output:  
Line Count: 3

Q7. Write a program to copy the content of one file into another.

Input:  
Content of source.txt:  
Copy this content.

Expected Output: 
File copied successfully.
Content of destination.txt:  
Copy this content.

Q8. Write a program to reverse the content of a file and save it to another file.

Input:  
Content of input.txt:  
ABC

Expected Output:  
Reversed content written to output.txt
Content of output.txt:  
CBA

Q9. Write a program to count the number of characters in a file.

Input:  
Content of chars.txt:  
Hello!

Expected Output:  
Character Count: 6

Q10. Write a program to read and display the content of a file line by line.

Input:  
Content of readline.txt:  
Line 1
Line 2
Line 3

Expected Output:  
Line 1
Line 2
Line 3

Q11. Write a program to search for a word in a file.

Input:  
Content of search.txt:  
C++ Programming
Search Word: Programming

Expected Output:  
Word found!

Q12. Write a program to write and read binary data to/from a file.

Input:  
Write: 1234  

Expected Output:  
Binary Data: 1234

Q13. Write a program to delete a file.

Input:  
File: delete_me.txt  

Expected Output:  
File deleted successfully.

Q14. Write a program to find the size of a file in bytes.

Input:  
Content of size.txt:  
Hello

Expected Output:  
File Size: 5 bytes

Q15. Write a program to count the number of vowels in a file.

Input:  
Content of vowels.txt:  
C++ is awesome!

Expected Output:  
Vowel Count: 5

Q16. Write a program to save an object of a class to a file.

Input:  
cpp
Name: John
Age: 25

Expected Output:  
Object written successfully.

Q17. Write a program to read an object of a class from a file.

Expected Output:  
Name: John
Age: 25

Q18. Write a program to count the number of digits in a file.

Input:  
Content of digits.txt:  
123ABC456

Expected Output:  
Digit Count: 6

Q19. Write a program to find the longest word in a file.

Input:  
Content of longword.txt:  
C++ is versatile.

Expected Output:  
Longest Word: versatile

Q20. Write a program to convert all characters in a file to uppercase and save to a new file.

Input:  
Content of lowercase.txt:  
hello

Expected Output:  
Converted content written to uppercase.txt
Content of uppercase.txt:  
HELLO

Q21. Write a program to sort the lines in a file alphabetically and save the sorted lines in a new file.

Input:  
Content of unsorted.txt:  
Banana  
Apple  
Cherry  

Expected Output:  
Sorted content written to sorted.txt
Content of sorted.txt:  
Apple  
Banana  
Cherry

Q22. Write a program to replace all occurrences of a specific word in a file with another word.

Input:  
Content of replace.txt:  
I love programming. Programming is fun.

Find: Programming  
Replace with: Coding

Expected Output:  
Words replaced successfully.
Updated file content:  
I love programming. Coding is fun.

Q23. Write a program to merge the content of two files into a new file.

Input:  
Content of file1.txt:  
Hello
Content of file2.txt:  
World

Expected Output:  
Files merged into merged.txt
Content of merged.txt:  
Hello
World

Q24. Write a program to extract and save every second line from a file to another file.

Input:  
Content of lines.txt:  
Line 1  
Line 2  
Line 3  
Line 4  

Expected Output:  
Specific lines saved to extracted.txt
Content of extracted.txt:  
Line 2  
Line 4  

Q25. Write a program to count the occurrences of a specific word in a file.

Input:  
Content of wordcount.txt:  
C++ is great. C++ is powerful.
Word to count: C++

Expected Output:  
Word 'C++' occurs 2 times.

Q26. Write a program to append timestamped messages to a log file.

Input:  
Message: System started.  

Expected Output:  
Log entry added.
Content of log.txt:  
[2024-12-02 10:00:00] System started.

Q27. Write a program to save tabular data to a CSV file.

Input:  
Rows of data:  
Name, Age, Grade  
Alice, 20, A  
Bob, 22, B  

Expected Output:  
Data written to students.csv
Content of students.csv:  
Name,Age,Grade  
Alice,20,A  
Bob,22,B

Q28. Write a program to read data from a CSV file and display it.

Input:  
Content of students.csv:  
Name,Age,Grade  
Alice,20,A  
Bob,22,B  

Expected Output:  
Data from students.csv:
Name: Alice, Age: 20, Grade: A  
Name: Bob, Age: 22, Grade: B

Q29. Write a program to encrypt file content using a simple substitution cipher and save it to another file.

Input:  
Content of plain.txt:  
HELLO

Expected Output:  
Encrypted content saved to encrypted.txt
Content of encrypted.txt:  
IFMMP

Q30. Write a program to decrypt an encrypted file using the same cipher.

Input:  
Content of encrypted.txt:  
IFMMP

Expected Output:  
Decrypted content saved to decrypted.txt
Content of decrypted.txt:  
HELLO

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!