LearnFrenzy
Sign in Take a free test

File Handling (fstream) · Exercise 4 of 4

File Handling (fstream) File Handling Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

C++ programs that read, write, update and organise files. Write each one yourself first, then open the solution – every solution was compiled and run (using the system temporary directory), and the output shown is the real output.

Question 1 Easy

Write save(students, path) and load(path) functions that store student records as one line each ("name,city,marks") and read them back into a vector; save, load and print the class topper.

Question page & discussion
Question 2 Medium

Read a text file word by word, normalise each word (lower-case, strip punctuation), count frequencies in a std::map and write the top words to a report file; print the report.

Question page & discussion
Question 3 Hard

Implement a tiny database of Product records (id, name[16], price, stock) in a binary file: append records, find one by id with seekg, update its stock in place, and list all records.

Question page & discussion
Question 4 Easy

Read an application log line by line, copy every line containing "ERROR" into errors.log with its line number, count lines per level (INFO, WARN, ERROR) and print a summary.

Question page & discussion
Question 5 Medium

Two files each contain sorted integers. Merge them into a third sorted file by reading one number at a time from each (without loading everything into memory), then print the result.

Question page & discussion
Question 6 Medium

Parse a config file with "key = value" lines, # comments and blank lines into a std::map; report malformed lines with their numbers and fall back to defaults for missing keys.

Question page & discussion
Question 7 Easy

Write a function stats(path) that returns lines, words, characters and the longest line of a text file, and run it on two generated files plus a missing one (reporting the error).

Question page & discussion
Question 8 Medium

Simulate three "runs" of an attendance program; each run appends the present students for that day to a log file with std::ios::app. Then read the log and print how many days each student attended.

Question page & discussion
Question 9 Hard

Given a folder of mixed files, create a subfolder per extension (pdf, jpg, txt, …) and move each file into it with std::filesystem::rename; print a sorted summary of the folders.

Question page & discussion
Question 10 Easy

Read all lines of a file into a vector, write them in reverse order with line numbers to a new file using std::format-style padding, and print the new file.

Question page & discussion
All exercises doneBack to File Handling (fstream)Revise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links