Reading and Writing CSV Files
Parse CSV files line by line and write results back to disk.
Reading and Writing CSV Files is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is CSV?
CSV (Comma-Separated Values) is a plain-text tabular format. Each line is a row; commas separate columns. Simple but full of edge cases.
Required Headers
Use fstream for files, sstream for parsing.
#include <fstream>
#include <sstream>
#include <string>
#include <vector>Opening a File for Reading
Construct an std::ifstream with the file path. Check that it opened.
std::ifstream file("data.csv");
if (!file) {
std::cerr << "Cannot open file\n";
return 1;
}Reading Line by Line
Use std::getline to iterate over rows.
std::string line;
while (std::getline(file, line)) {
process(line);
}Splitting a Row
Use a stringstream and getline with a delimiter to split each row into fields.
std::vector<std::string> split(const std::string& line) {
std::vector<std::string> fields;
std::stringstream ss(line);
std::string field;
while (std::getline(ss, field, ',')) {
fields.push_back(field);
}
return fields;
}Skipping the Header
Read and discard the first line if it contains column names.
std::string header;
std::getline(file, header); // discardParsing Typed Fields
Convert string fields to typed values with std::stoi or std::stod.
auto fields = split(line);
int id = std::stoi(fields[0]);
double price = std::stod(fields[2]);Writing CSV
Use std::ofstream to write. Stream values with << and add commas and newlines.
std::ofstream out("results.csv");
out << "name,score\n";
out << "Ada,95\n";
out << "Bob,82\n";Quoting Fields with Commas
If a field contains a comma, wrap it in double quotes. To include a quote inside the field, double it.
std::string escape(const std::string& s) {
if (s.find_first_of(",\"\n") == std::string::npos) return s;
std::string r = "\"";
for (char c : s) {
if (c == '"') r += '"';
r += c;
}
return r + "\"";
}Handling Newlines in Fields
Quoted fields can span multiple physical lines. A robust CSV reader tracks quote state across getline calls. For simple cases, stick to non-multiline fields.
Closing the File
File streams close automatically when destroyed (RAII). For an explicit close use file.close() — only needed when you want to reuse the stream.
When to Use a Library
For complex CSV files (quoted commas, multiline values, encoding), use a library like rapidcsv or csv-parser. Roll your own only for simple, controlled formats.
Quick Check
What technique splits a line on commas using only the standard library?
Recap
Read CSV with std::ifstream + std::getline, split rows with a stringstream and a comma delimiter, and parse typed fields with std::stoi/stod. Use a real library for complex CSV.
Frequently asked questions
Is the “Reading and Writing CSV Files” lesson free?
Yes — the full text of “Reading and Writing CSV Files” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading and Writing CSV Files”?
Parse CSV files line by line and write results back to disk. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading and Writing CSV Files” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building a Simple Calculator CLI
- Reading and Writing CSV Files
- A Number Guessing Game
- A Word Frequency Counter