Get a string in a text CSV file in C ++

I have a large CSV (~ 75 MB) of this type:

1,3,4.1,5.4 -2,-4,-0.1,-11.3 ... 

And I save my data with this code (C style) :

 #include <iostream> #include <cstdio> #include <vector> int main() { int s; int x; float y; double z; std::vector<int> t; std::vector<int> u; std::vector<float> v; std::vector<double> w; if (std::FILE *f = std::fopen("data.csv", "r")) { while (std::fscanf(f, "%d,%d,%f,%lf", &s, &x, &y, &z) == 4) { t.push_back(s); u.push_back(x); v.push_back(y); w.push_back(z); } std::fclose(f); } return 0; } 

And that took me in this big CSV (~ 75 MB):

 real 0m3.195s user 0m3.032s sys 0m0.148s 

It's so fast in C style!

Another way is using this code (C ++ style) :

 #include <iostream> #include <fstream> #include <vector> int main() { char c; // to eat the commas. Not eat spaces :-( int s; int x; float y; double z; std::vector<int> t; std::vector<int> u; std::vector<float> v; std::vector<double> w; std::ifstream file("data.csv"); while (file >> s >> c >> x >> c >> y >> c >> z) { t.push_back(s); u.push_back(x); v.push_back(y); w.push_back(z); } return 0; } 

And that took me in this big CSV (~ 75 MB):

 real 0m4.766s user 0m4.660s sys 0m0.088s 

Style C is faster!

I would like to read a row in the first column (or in the second) and put in the std::string vector.

I try many features (char *, iostream, etc.), but I can not do it quickly and elegantly.

Examples of types of large CSV files (is there something easier to read than another?):

a.csv:

 hi,3,4.1,5.4 hello,-4,-0.1,-11.3 ... 

b.csv:

 hi 3 4.1 5.4 hello -4 -0.1 -11.3 ... 

c.csv:

 "hi",3,4.1,5.4 "hello",-4,-0.1,-11.3 ... 

d.csv:

 "hi" 3 4.1 5.4 "hello" -4 -0.1 -11.3 ... 

Thank you for help!:)

+1
source share
1 answer

So, are you looking for a more efficient way to do this? Well, one thing you could do is consider whether you really need vectors. Depending on your use, you might be better off with some sort of linked list structure.

0
source

All Articles