Programming foundations

Stage 1 · Lesson 6 of 8

Reading files and parsing input

Your first language

Most real programs load data from files and then need : turning lines of text into numbers and fields. in C++ starts with std::ifstream in("data.csv");, and you check if (!in) before using it. A missing file does not throw an error by default; it just leaves the stream in a failed state.

Read line by line with while (std::getline(in, line)). To split a line, wrap it in a std::istringstream and call getline with a separator such as a comma. Convert text to numbers with std::stoi or std::stod, which throw on bad input.

are worked out from the folder you run the program in, not where the source is. Mixing std::cin >> x with getline is a classic trap: the leftover newline makes the next getline return an empty line.

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

  • CourseFree
    LearnCpp.com

    LearnCpp.com · Beginner

    Free, current and careful: chapters 0 to 16 take you from setup to references and std::vector

Practise

  • Small exercises in your language track, with free feedback from a human mentor

Keep handy

  • The standard library reference: check std::vector and std::string pages as you use them

  • Quick syntax lookups and an online compiler for testing a snippet on a shared computer

Mark this stage as not done?