Hey guys im trying to find a specific line or word from a text file and i cant seem to figure out how to do that. Now i want to find the destination from a text file the destination can be for example New York, Washington,or Texas now i want to read from the text file and find one of these destinations and by that i mean that the user will pick one of the above mentioned destinations which is then stored in a text file and then i need to read from the text file and store it in a string variable and display it. Here is what i have so far..
1 2 3 4 5 6 7 8 9 10 11
// Creating a ifstream object to read from the text file data.txt...
ifstream readFromFile("Data.txt",ios::in);
// Creating a string variable.
string data;
while(!readFromFile.eof()){ // I cant figure out what to add here so that the code will find and store the destination.
getline(readFromFile,data);
}
cout << data;
It's from cstring, so I don't know if it would work with a c++ string, I usually steer away from IOstream and towards cstdio, it's faster and in my opinion, easier to work with.
string Destinations[3] = {"New York", "Washington", "Texas"};
// Creating a ifstream object to read from the text file data.txt...
ifstream readFromFile("Data.txt");
// Creating a string variable.
string data;
while(!readFromFile.eof()){ // I cant figure out what to add here so that the code will find and store the destination.
getline(readFromFile,data);
for (int i = 0; i < 3; i++)
if (data.find(Destinations[i])!=string::npos) cout << Destinations[i] << endl;
}