The structured text that you read from a file comes as a line at a time, and each line has to be broken into its fields. The tools for that are the string search and the substring, the string stream, and a careful eye on the quoted fields and the trailing whitespace.
The previous lesson in this module covered "How Do ifstream and ofstream Read and Write Files?", and the next lesson in this module is about "What Do Binary Files Change About IO?".
Splitting on a delimiter
The std::string::find gives you the position, and the std::string::substr takes the piece.
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> split(const std::string& text, char delimiter) {
std::vector<std::string> fields;
std::string::size_type start = 0;
while (true) {
std::string::size_type position = text.find(delimiter, start);
if (position == std::string::npos) {
fields.push_back(text.substr(start));
break;
}
fields.push_back(text.substr(start, position - start));
start = position + 1;
}
return fields;
}
int main() {
std::string line = "m-001|Alice|2024";
for (const std::string& field : split(line, '|')) {
std::cout << "[" << field << "]\n";
}
return 0;
}The output shows the line split into its three fields.
[m-001]
[Alice]
[2024]Reading whitespace-separated data
When the fields are separated by the runs of the whitespace rather than by a single character, then a string stream does the work for you.
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string line = " 12 34.5 frost ";
std::istringstream stream(line);
int count = 0;
double weight = 0.0;
std::string condition = "unknown";
if (stream >> count >> weight >> condition) {
std::cout << "count: " << count << '\n';
std::cout << "weight: " << weight << '\n';
std::cout << "condition: " << condition << '\n';
} else {
std::cout << "could not parse the line\n";
}
return 0;
}The output shows the three fields extracted, with the leading and the trailing whitespace skipped automatically.
count: 12
weight: 34.5
condition: frostHandling quoted CSV fields
A plain split on the comma gets this wrong, because a quoted field may hold a comma of its own.
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> parse_csv_line(const std::string& line) {
std::vector<std::string> fields;
std::string current;
bool in_quotes = false;
for (std::size_t i = 0; i < line.size(); ++i) {
char c = line[i];
if (in_quotes) {
if (c == '"') {
if (i + 1 < line.size() && line[i + 1] == '"') {
current += '"'; // an escaped quote
++i;
} else {
in_quotes = false;
}
} else {
current += c;
}
} else if (c == '"') {
in_quotes = true;
} else if (c == ',') {
fields.push_back(current);
current.clear();
} else {
current += c;
}
}
fields.push_back(current);
return fields;
}
int main() {
std::string line = "m-001,\"Smith, Alice\",2024";
for (const std::string& field : parse_csv_line(line)) {
std::cout << "[" << field << "]\n";
}
return 0;
}The output shows the quoted field kept together, with the comma inside it.
[m-001]
[Smith, Alice]
[2024]Converting fields to numbers
The std::from_chars is the most reliable way to convert the text to a number, because it tells you exactly how much it consumed and it never throws.
#include <charconv>
#include <iostream>
#include <optional>
#include <string>
std::optional<int> to_int(const std::string& text) {
int value = 0;
const char* first = text.data();
const char* last = text.data() + text.size();
const auto result = std::from_chars(first, last, value);
if (result.ec != std::errc{} || result.ptr != last) {
return std::nullopt;
}
return value;
}
int main() {
std::cout << "42 -> " << to_int("42").value_or(-1) << '\n';
std::cout << "4x -> " << to_int("4x").value_or(-1) << '\n';
std::cout << " -> " << to_int("").value_or(-1) << '\n';
return 0;
}The output shows the clean number parsed and the two malformed ones rejected.
42 -> 42
4x -> -1
-> -1Trimming and normalising
The fields that are read from a file often carry the stray whitespace, and trimming them before you compare them is worth doing once in a helper.
#include <cctype>
#include <iostream>
#include <string>
std::string trim(const std::string& text) {
const std::string spaces = " \t\r\n";
std::size_t start = text.find_first_not_of(spaces);
if (start == std::string::npos) {
return "";
}
std::size_t end = text.find_last_not_of(spaces);
return text.substr(start, end - start + 1);
}
int main() {
std::cout << "[" << trim(" spaced ") << "]\n";
std::cout << "[" << trim("\t\ttabs\t") << "]\n";
std::cout << "[" << trim(" ") << "]\n";
return 0;
}The output shows the three cases.
[spaced]
[tabs]
[]Reading a whole file into a structure
Putting the pieces together gives a small file reader that turns the lines into the records.
#include <charconv>
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
struct Member {
std::string id;
std::string name;
int year;
};
std::optional<int> to_int(const std::string& text) {
int value = 0;
const char* first = text.data();
const char* last = text.data() + text.size();
const auto result = std::from_chars(first, last, value);
if (result.ec != std::errc{} || result.ptr != last) {
return std::nullopt;
}
return value;
}
std::vector<Member> read_members(const std::string& path) {
std::vector<Member> members;
std::ifstream file(path);
if (!file) {
return members;
}
std::string line;
while (std::getline(file, line)) {
std::string id;
std::string name;
std::string year_text;
std::istringstream fields(line);
std::getline(fields, id, '|');
std::getline(fields, name, '|');
std::getline(fields, year_text, '|');
auto year = to_int(year_text);
if (year.has_value()) {
members.push_back(Member{id, name, *year});
}
}
return members;
}
int main() {
std::vector<Member> members = read_members("members.txt");
std::cout << "loaded " << members.size() << " members\n";
for (const Member& member : members) {
std::cout << member.id << ' ' << member.name << ' ' << member.year << '\n';
}
return 0;
}The output shows the file parsed into the records.
loaded 4 members
m-001 Alice 2024
m-002 Bob 2025
m-003 Carol 2026
m-004 Dave 2026Quick reference
- The
findgives you the position and thesubstrtakes the piece, and that is the manual split. - The string stream splits on the runs of the whitespace for you.
- The quoted fields need a small state machine, because the delimiter may be inside the quotes.
- You use the
std::from_charsto convert the text, and you check that the whole text was consumed. - You trim the fields once, in a helper, before you compare them.
- You keep the parsing and the checking apart, and you turn the lines into the records in one place.
Try it yourself: write a splitter that breaks a line on the pipe and that keeps the empty fields. Then write one that handles the quoted fields, and check that the field with the comma inside it comes out whole. Finally, read a file of those lines and turn each one into a record, and skip the lines that do not have the right number of fields.