c++ - A file reading function -
i need write function read file, , ideally store strings file vector
of string
s.
so far have this:
void loadcars() { fstream carfile; string car; carfile.open("car.txt"); if (carfile.is_open()) { while (getline(carfile, car)) { cout << car << "\n"; } carfile.close(); } else { cout << "unable open file" << endl; } }
can me, need function read text file contains 4 strings:
car1
car2
car3
car4
and want read them , put them vector ideally. cout
purely me trying test works, unable open file.
and ideally store strings file vector of strings.
just extend function follows, collect results in vector:
void loadcars(std::vector<std::string>& result) { result.clear(); fstream carfile; string car; carfile.open("car.txt"); if (carfile.is_open()) { while (getline(carfile, car)) { result.push_back(car); cout << car << "\n"; } carfile.close(); } else { cout << "unable open file" << endl; } }
as problems opening file, might happen because file isn't available in programs current working directory (check debug settings), or don't have access right on it.
Comments
Post a Comment