在C++中添加更多的文字到文本文件

在C++中添加更多的文字到文本文件,c++,file,text,add,words,C++,File,Text,Add,Words,我正在创建一个程序,将用户输入的单词写入一个已经有其他单词开始的文本文件。所以像这样: "words.txt" apples oranges bananas 我想做的是在列表中添加其他单词,然后在屏幕上输出所有单词。我写了一个程序,但它不会输入用户指定的单词 int main(){ ifstream fin("wordlist.txt"); if (fin.fail()){ cerr << "Error opening the file" <

我正在创建一个程序,将用户输入的单词写入一个已经有其他单词开始的文本文件。所以像这样:

"words.txt"
apples
oranges
bananas
我想做的是在列表中添加其他单词,然后在屏幕上输出所有单词。我写了一个程序,但它不会输入用户指定的单词

int main(){

    ifstream fin("wordlist.txt");

    if (fin.fail()){
        cerr << "Error opening the file" << endl;
        system("pause");
        exit(1);
    }

    vector<string> wordlist;
    string word;

    string out_word;

    cout << "Please enter a word: ";
    cin >> out_word;

    fin >> out_word;    //Trying to input the user specified word

    //This inputs all the words
    while (!fin.eof()){
        fin >> word;
        wordlist.push_back(word);
    }

    //This outputs all the words on the screen
    for (int i = 0; i < wordlist.size(); i++){
        cout << wordlist[i] << endl;
    }

    fin.close();
    system("pause");
    return 0;
}

处理此问题的简单方法:

将文件读入向量 询问用户单词并添加到向量中 写出向量的所有单词到输出流。
首先!fin.eof应该是whilefin>>字这到底是什么?仅仅查找如何向文件写入或从文件写入是否太困难。投票结束:这个问题是由一个无法再复制的问题或一个简单的印刷错误引起的。虽然类似的问题可能在这里的主题,这是一个解决的方式不太可能帮助未来的读者。这通常可以通过在发布前确定并仔细检查重现问题所需的最短程序来避免。在这种情况下,我认为最好只使用stringstream,因为他们不会再次尝试访问/编辑单词。因此只需按原样添加它们enough@Jidder当前位置我不知道电话号码在哪里stringstream会合适的。我只是想说明,在许多情况下,读修改写是处理文件的最简单方法,而不是在代码中编辑文件。