在 C++ 的文本文件中添加更多单词
Add more words to a text file in C++
我正在创建一个程序,该程序将用户输入的单词写入已经有其他单词的文本文件。 所以像这样:
"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;
}
处理此问题的简单方法:
- 将文件读入向量
- 询问用户单词并添加到向量中
- 将向量的所有单词写出到外流。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- C++为一串单词添加空格
- 在单词之间添加空格
- 字数统计函数在将单词添加到一组唯一单词时遇到问题
- 从之前添加的批处理文件中删除单词
- 如何搜索向单词添加字母的所有可能组合?
- 如何读取文件的内容,如果文件中的单词重复,请将它们添加到列表中?
- 如何在两个单词之间添加空格
- Xapian - 搜索查询不检索匹配项,除非将""添加到每个单词
- 当循环崩溃时,不向数组添加单词
- 奇怪的添加 - 计算单词在文件中出现的次数
- 使用单词键将多个行号的向量添加到我的地图中
- 在文本文件内预先存在的字符串前面添加一个单词
- 计算输入文件中的单词,添加额外的单词
- 在 C++ 的文本文件中添加更多单词
- 在字符串数组中添加单词
- 给定一个单词,通过在它们之间添加空格来形成一个有意义的单词
- 从文件中读取并有效地向树中添加单词
- 读取一个单词序列并将它们添加到一个向量中