+ 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 */
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.
+ 1
One of the possible variants to fix your code is to replace (element = "bar") to (element = "\"bar\"")
0
its working now, thanks for the help man!