Stage 6 · Lesson 3 of 7
Files and parsing input
A C++ usually reads a text file, such as a CSV of transactions, and turns each line into a struct (a small record type). uses std::ifstream and std::getline, and you split the fields with std::istringstream.
Convert text with std::stoi or std::stod, which std::invalid_argument on bad text. Catch that exception and report the line number, so users can fix their file. Decide what a bad line means: skip it with a warning, or stop.
Check that the stream opened, and remember that are worked out from where the program runs. Keep parsing in its own that returns a std::vector of structs, so the core logic and the tests never touch files.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with
- GitHub repoFreeProject Based Learning
Community maintained · Beginner
Follow one guided build in your language, then build your own without a guide
Keep handy
Also worth your time
How to present each project: the problem, stack, challenges and results
- Build Your Own XGo deeper
When you want a harder build: a shell, a key-value store or a tiny Git, from scratch