Skip to content

Commit 12e5a94

Browse files
read_write_get_set.cpp
1 parent 51b531c commit 12e5a94

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

rapidcsv/read_write_get_set.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "rapidcsv.h"
2+
#include <filesystem>
3+
#include <iostream>
4+
5+
int main() {
6+
//read
7+
rapidcsv::Document doc("test.csv", rapidcsv::LabelParams(0, 0));
8+
9+
//get
10+
int kevin_grade, jack_grade;
11+
std::string steven_parent;
12+
13+
kevin_grade = doc.GetCell<int>("grade", "Kevin");
14+
jack_grade = doc.GetCell<int>("grade", "Jack");
15+
steven_parent = doc.GetCell<std::string>("parent", "Steven");
16+
17+
std::cout << "Kevin's grade: " << kevin_grade << std::endl;
18+
std::cout << "Jack's grade: " << jack_grade << std::endl;
19+
std::cout << "Steven's parent: " << steven_parent << std::endl;
20+
21+
//set
22+
doc.SetCell<int>("grade", "Marry", 80);
23+
doc.SetCell<std::string>("parent", "Apple", "Antony");
24+
25+
//write
26+
std::string outfolder = std::filesystem::current_path().string();
27+
if (!outfolder.empty()) outfolder += "\\";
28+
std::string outpath = outfolder + "overwrite.csv";
29+
std::cout << "output to: " << outpath << std::endl;
30+
std::ofstream ostream;
31+
ostream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
32+
ostream.open(outpath, std::ios::binary | std::ios::ate);
33+
doc.Save(ostream);
34+
ostream.close();
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)