Video Coming Soon...
20: Input From a File
Reading and Using a diff
Use filesystem
Version 1: Get the Data
View Source file ex20_v1.cpp Only
#include <fmt/core.h>
#include <fstream>
#include <vector>
using std::vector, std::string, std::ifstream, std::ios;
using fmt::println;
int chunk_number(const string& line, size_t start, size_t end) {
string chunk = line.substr(start, end - start);
return stoi(chunk);
}
vector<int> parse_line(const string& line) {
vector<int> result;
size_t start = 0;
size_t end = line.find(",", start);
while(end != string::npos) {
int as_number = chunk_number(line, start, end);
result.push_back(as_number);
start = end + 1;
end = line.find(",", start);
}
// need to get the final chunk
int as_number = chunk_number(line, start, end);
result.push_back(as_number);
return result;
}
int main(int argc, char* argv[]) {
string filename{argv[1]};
ifstream in_file(filename, std::ios::binary);
if(!in_file.is_open()) {
println("failed to open {}", filename);
return 1;
}
string line;
while(in_file) {
getline(in_file, line);
if(line == "") break;
vector<int> row = parse_line(line);
for(int cell : row) {
println("number: {}", cell);
}
}
}
Version 2: Vector of Vector Tables
View Source file ex20_v1_v2.diff Only
--- ex20_v1.cpp 2026-04-12 10:36:26.108174362 -0400
+++ ex20_v2.cpp 2026-04-12 10:36:49.666521626 -0400
@@ -3,7 +3,7 @@
#include <vector>
using std::vector, std::string, std::ifstream, std::ios;
-using fmt::println;
+using fmt::println, fmt::print;
int chunk_number(const string& line, size_t start, size_t end) {
string chunk = line.substr(start, end - start);
@@ -33,6 +33,7 @@
int main(int argc, char* argv[]) {
string filename{argv[1]};
ifstream in_file(filename, std::ios::binary);
+ vector<vector<int>> table;
if(!in_file.is_open()) {
println("failed to open {}", filename);
@@ -47,9 +48,14 @@
if(line == "") break;
vector<int> row = parse_line(line);
+ table.push_back(row);
+ }
+ for(vector<int> row : table) {
for(int cell : row) {
- println("number: {}", cell);
+ print("{}\t", cell);
}
+
+ print("\n");
}
}
Version 3: Type Aliases
View Source file ex20_v2_v3.diff Only
--- ex20_v2.cpp 2026-04-12 10:36:49.666521626 -0400
+++ ex20_v3.cpp 2026-04-12 10:36:57.531303795 -0400
@@ -5,13 +5,16 @@
using std::vector, std::string, std::ifstream, std::ios;
using fmt::println, fmt::print;
+using Row = vector<int>;
+using Table = vector<Row>;
+
int chunk_number(const string& line, size_t start, size_t end) {
string chunk = line.substr(start, end - start);
return stoi(chunk);
}
-vector<int> parse_line(const string& line) {
- vector<int> result;
+Row parse_line(const string& line) {
+ Row result;
size_t start = 0;
size_t end = line.find(",", start);
@@ -32,8 +35,8 @@
int main(int argc, char* argv[]) {
string filename{argv[1]};
- ifstream in_file(filename, std::ios::binary);
- vector<vector<int>> table;
+ ifstream in_file(filename, ios::binary);
+ Table table;
if(!in_file.is_open()) {
println("failed to open {}", filename);
@@ -47,11 +50,11 @@
if(line == "") break;
- vector<int> row = parse_line(line);
+ Row row = parse_line(line);
table.push_back(row);
}
- for(vector<int> row : table) {
+ for(Row row : table) {
for(int cell : row) {
print("{}\t", cell);
}
Register for Learn C++ the Hard Way
Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.