-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcsv_reading_example.cpp
More file actions
76 lines (66 loc) · 2.17 KB
/
csv_reading_example.cpp
File metadata and controls
76 lines (66 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Demo of fast-cpp-csv-parser, matching docs/csv.md.
//
// We write a tiny CSV to /tmp at runtime, then read it back twice:
// 1. With a plain try/catch (first snippet in the doc).
// 2. With specific io::error::* catch blocks (second snippet in the doc).
#define CSV_IO_NO_THREAD
#include <csv.h>
#include <fstream>
#include <iostream>
#include <string>
// Define a structure to hold the data
struct Position {
double x;
double y;
double z;
};
static const std::string filepath = "/tmp/positions.csv";
void write_sample_csv() {
std::ofstream out(filepath);
out << "x,y,z\n";
out << "1.0,2.0,3.0\n";
out << "4.5,5.5,6.5\n";
out << "7.0,8.0,9.0\n";
}
void demo_basic_try_catch() {
std::cout << "--- demo 1: basic try/catch ---\n";
try {
io::CSVReader<3, io::trim_chars<' '>, io::double_quote_escape<',', '\"'>>
in(filepath);
in.read_header(io::ignore_missing_column | io::ignore_extra_column, "x",
"y", "z");
Position position;
while (in.read_row(position.x, position.y, position.z)) {
std::cout << "x: " << position.x << ", y: " << position.y
<< ", z: " << position.z << "\n";
}
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
void demo_specific_exceptions() {
std::cout << "--- demo 2: specific exception types ---\n";
try {
io::CSVReader<3, io::trim_chars<' '>, io::double_quote_escape<',', '\"'>>
in(filepath);
in.read_header(io::ignore_missing_column | io::ignore_extra_column, "x",
"y", "z");
Position position;
while (in.read_row(position.x, position.y, position.z)) {
std::cout << "x: " << position.x << ", y: " << position.y
<< ", z: " << position.z << "\n";
}
} catch (const io::error::can_not_open_file &e) {
std::cerr << "Error: Can't open file. Reason: " << e.what() << "\n";
} catch (const io::error::base &e) {
std::cerr << "Error: CSV parsing error. Reason: " << e.what() << "\n";
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
int main() {
write_sample_csv();
demo_basic_try_catch();
demo_specific_exceptions();
return 0;
}