Add more words to a text file in C++ -
i'm creating program writes user inputted words text file has other words begin with. this:
"words.txt" apples oranges bananas
what wanna add other words list , output words on screen. wrote program won't input user specified word.
int main(){ ifstream fin("wordlist.txt"); if (fin.fail()){ cerr << "error opening file" << endl; system("pause"); exit(1); } vector<string> wordlist; string word; string out_word; cout << "please enter word: "; cin >> out_word; fin >> out_word; //trying input user specified word //this inputs words while (!fin.eof()){ fin >> word; wordlist.push_back(word); } //this outputs words on screen (int = 0; < wordlist.size(); i++){ cout << wordlist[i] << endl; } fin.close(); system("pause"); return 0; }
a simple way handle this:
- read in file vector
- ask user words , add vector
- write out words of vector out-stream.
Comments
Post a Comment