aboutsummaryrefslogtreecommitdiff
path: root/src/static/Source.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/static/Source.cpp')
-rw-r--r--src/static/Source.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/static/Source.cpp b/src/static/Source.cpp
new file mode 100644
index 0000000..0bba7c6
--- /dev/null
+++ b/src/static/Source.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <vector>
+
+int main(int argc, char** argv) {
+
+ std::ifstream f;
+ f.open("out.csv"); /* open file with filename as argument */
+ if (!f.is_open()) { /* validate file open for reading */
+ std::cerr << "error: file open failed '" << argv[1] << "'.\n";
+ return 0;
+ }
+
+
+ std::string line, val; /* string for line & value */
+ std::vector<std::vector<int>> array; /* vector of vector<int> */
+
+ while (std::getline(f, line)) { /* read each line */
+ std::vector<int> v; /* row vector v */
+ std::stringstream s(line); /* stringstream line */
+ while (getline(s, val, ',')) /* get each value (',' delimited) */
+ v.push_back(std::stoi(val)); /* add to row vector */
+ array.push_back(v); /* add row vector to array */
+ }
+
+ std::vector<int> flat_array; /* create 1D vector */
+ for (auto& row : array) { /* iterate over rows */
+ for (auto& val : row) /* iterate over vals */
+ flat_array.push_back(val); /* add to 1D vector */
+ }
+
+ int* array1d = flat_array.data(); /* get pointer to data */
+ int size = flat_array.size(); /* get size of 1D array */
+
+ std::ofstream out_file("out.bin", std::ios::binary); /* create binary file */
+ out_file.write((char*)&size, sizeof(size)); /* write size of 1D array to file */
+ out_file.write((char*)array1d, size * sizeof(int)); /* write 1D array to file */
+ out_file.close(); /* close file */
+
+ return 0;
+}