在 C++ 中将字符串附加到文件

Appending strings to a file in C++

我一直在使用 C++ 进行文件输出和输入时遇到问题。出于某种原因,我就是无法理解。但我有一个问题(是的,对于 class.. 我知道。我已经感觉很糟糕,但教授没有太大帮助)关于我正在为菜单工作的这个功能。

这是我目前所知道的。

void addName(){
string name;
ofstream outfile;
cout << "Please type a name you would like to add:";

outfile.open("file.txt", std::ios_base::app);
cin >> name;
outfile << name;
cout << "Name added!" << endl;
}

我将其纳入我的 main 事物。对不起我的业余性。

问题是如何将 "names" 附加到我的 file.txt

截图:

http://gyazo.com/24fe08435a6a1427de619892f2b28d1f

http://gyazo.com/4aa58fee549aba6771bcbc6811542f91

您可能看错了文件。您的文件将位于您的源代码所在的位置。而且不在 DebugRelease 文件夹中。