+ 1

Why its not working? it should show "Found bar".

#include <iostream> #include <fstream> #include <string> #include <sstream> int main(){ std::ifstream file("mycsv.csv"); std::string line, word; std::string element = "bar"; while (getline(file, line)){ std::stringstream strstr(line); while(getline(strstr, word, '\t')){ If(word.compare(element) == 0) { std::cout << "Found bar\n"; } } } return 0; } /* inside mycsv.csv: "foo" 123 346 "bar" 455 6567 */

2nd Feb 2017, 6:26 PM
Jhonnatha Andrade
Jhonnatha Andrade - avatar
3 Antworten
+ 1
In your input you have word "bar" and it's quoted. String named element in your code equals to bar without quotes. That's why your code is not working correctly.
2nd Feb 2017, 7:17 PM
Rustem Husnutdinov
Rustem Husnutdinov - avatar
+ 1
One of the possible variants to fix your code is to replace (element = "bar") to (element = "\"bar\"")
2nd Feb 2017, 7:21 PM
Rustem Husnutdinov
Rustem Husnutdinov - avatar
0
its working now, thanks for the help man!
2nd Feb 2017, 7:34 PM
Jhonnatha Andrade
Jhonnatha Andrade - avatar