c++ - A file reading function -


i need write function read file, , ideally store strings file vector of strings.

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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -