How to count strings from a file into a string array or into a vector in C ++? I have a file where paths to some files are recorded on each line. How to write all these paths into a string array or into a vector?

  • What about vector strings?) - free_ze
  • @free_ze I don't mind) - user200355

2 answers 2

A simple option without reading errors:

std::vector<std::string> lines; { std::ifstream f("file.txt"); std::string line; while (std::getline(f, line)) lines.push_back(line); } 
     std::vector<std::string> v; std::ifstream input; input.open("file.txt"); while (!input.eof()) { std::string s; std::getline(input, s); v.push_back(s); } input.close(); 
    • while (!input.eof()) - and here you get the crap in the last (extra) line ... - Harry