C++: Read CSV-file separated by ; AND \n -


this question has answer here:

sorry if it's pure stupidity i'm stuck @ file reading problem via c++. csv data i'd read:

5;1;0;3;3;5;5;3;3;3;3;2;3;3;0 5;1;0;3;3;5;0;3;3;3;3;2;0;0;3 5;1;1;3;3;0;0;0;0;3;5;2;3;3;3 0;3;5;5;0;2;0;3;3;0;5;1;1;0;0 0;0;3;5;5;2;0;0;0;0;5;5;1;1;0 0;0;0;0;5;2;0;0;0;0;0;5;5;1;0 ;;;;;;;;;;;;;; code;bezeichnung;kosten;;;;;;;;;;;; 0;ebene;6;;;;;;;;;;;; 1;fluss;10; (begrenzt nutzbar);;;;;;;;;;; 2;weg;2;;;;;;;;;;;; 3;wald;8;;;;;;;;;;;; 4;brücke;5;;;;;;;;;;;; 5;felswand;12;;;;;;;;;;;; 

here, i'd read first values (separated ;;;;) , store in 2 dimensional array. not problem if seperated ';'. if use

while (getline(csvread, s, ';')) { [...] } 

i information this: {5}{1}{0}{3}{3}{5}{5}{3}{3}{3}{3}{2}{3}{3}{0\n5}{1} saves newline , not think of delimitator.

so there option use getline if have 2 delimitators? or off? thought reading line line string, adding ; string , rewriting in file in order reuse getline using ;. can't best option, right?

you can use splitting function :

std::vector<std::string> split(const std::string& source, const std::string& delimiter){     std::vector<std::string> result;      size_t last = 0;     size_t next = 0;      while ((next = source.find(delimiter, last)) != std::string::npos){          result.push_back(source.substr(last, next - last));         last = next + delimiter.length();     }      result.push_back(source.substr(last));     return result; } 

now simply:

std::vector<std::vector<std::string>> parsedcsv; while (getline(csvread, s, '\n')) { parsedcsv.push_back(split(s,";")); } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -