w3resource

C++ File Handling: Exercises, Practice, Solution


This resource offers a total of 75 C++ File handling problems for practice. It includes 15 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Create and Write to a Text File

Write a C++ program to create a new text file and write some text into it.

Click me to see the solution


2. Open and Display File Contents

Write a C++ program to open an existing text file and display its contents on the console.

Click me to see the solution


3. Count the Number of Lines in a Text File

Write a C++ program to count the number of lines in a text file.

Click me to see the solution


4. Count the Number of Words in a Text File

Write a C++ program to count the number of words in a text file.

Click me to see the solution


5. Copy Contents of One Text File to Another

Write a C++ program to copy the contents of one text file to another.

Click me to see the solution


6. Find and Replace a Specific Word in a Text File

Write a C++ program to find and replace a specific word in a text file.

Click me to see the solution


7. Append New Data to an Existing Text File

Write a C++ program to append new data to an existing text file.

Click me to see the solution


8. Sort Lines in a Text File Alphabetically

Write a C++ program to sort the lines of a text file in alphabetical order.

Click me to see the solution


9. Merge Multiple Text Files into One

Write a C++ program to merge multiple text files into a single file.

Click me to see the solution


10. Split a Large Text File into Smaller Files

Write a C++ program to split a large text file into smaller files of equal size.

Click me to see the solution


11. Search for a Specific String in a Text File and Display Line Numbers

Write a C++ program to search for a specific string in a text file and display its line number(s).

Click me to see the solution


12. Encrypt Text File Using a Simple Algorithm

Write a C++ program to encrypt the contents of a text file using a simple encryption algorithm.

Click me to see the solution


13. Decrypt Text File Encrypted by the Above Algorithm

Write a C++ program to decrypt the contents of a text file encrypted using the above algorithm.

Click me to see the solution


14. Read a CSV File and Display Its Contents in Tabular Form

Write a C++ program to read a CSV file and display its contents in tabular form.

Click me to see the solution


15. Calculate the Average of Numbers Stored in a File

Write a C++ program to calculate the average of numbers stored in a file.

Click me to see the solution


CPP Code Editor:



More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.