Reputation: 91
I have a piece of code where i read a CSV file and prints it contents. Now the issue is that I want to run my code on ESP32 and because of some limitations of micropython I can't upload my file in the spiffs storage. So is there any way I can store the csv file content in the code itself instead of reading it from the outside?
My code:
vector<vector<double> > read_csv(const char *filename)
{
ifstream read_file(filename);
vector<vector<double> > csv_data;
std::string lineStr;
while (getline(read_file, lineStr))
{
stringstream ss(lineStr);
std::string d;
vector<double> a_line_data;
while (getline(ss, d, ','))
{
a_line_data.push_back(atof(d.c_str()));
}
csv_data.push_back(a_line_data);
}
//cout << csv_data << endl;
return csv_data;
}
This is how i print the contents:
vector<vector<double> > csv_data = read_csv("A00068.csv");
// print tests
printf("CSV:%d\n", csv_data);
printf("SIZE:%d\n", csv_data.size());
Upvotes: 1
Views: 188
Reputation: 123450
The most obvious possiblities are ....
Hardcode the values directly in your code:
std::vector<std::vector<double>> csv_data{ { 0.2,....},{....}, ...};
Another possiblity is to include your text file as string. For details I refer you to this answer: https://stackoverflow.com/a/25021520/4117728. You basically would add some token in the beginning of the file and at the end, the linked answer suggests this:
R"=====( 0.2, 0.1, ... your numbers
... more numbers...
0.42, 3.141)====="
Then include the file like this:
std::string csv_contents =
#include "test.txt"
;
Now you have a string that you need to parse just as you had to parse the file contents before. Hence the only advantage is that now your data can be embedded with the executable instead of being placed in a seperate file. If you worry about cost of doing the parsing then you might consider the alternative mentioned first.
Upvotes: 1