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. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Reading and Writing CSV Files” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Reading and Writing CSV Files”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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