c++输出的问题

C++ Probllem with output

本文关键字:问题 输出 c++      更新时间:2023-10-16

我需要用户键入他想要写入数据的文件。例子:你想写什么文本文件"Hello Too"?他输入to: example,数据将进入example.txt文件,其中包含Hello Too。问题是,它不能采取一个字符串(accname),并结合。txt,使它发送的数据也文本文件。这很难解释,希望你能理解:D

int Transfersum;
string accname;
cout << "Type the account name you want to send too" << endl;
cin >> accname;
ofstream mfile;
mfile.open ( accname ".txt");
mfile << Transfersum;
mfile.close();

您必须连接accname + ".txt"以将其用作mfile.open ()调用的参数表达式。

您应该能够使用+的字符串连接。

mfile.open ( accname + ".txt");

如果你不是在c++ 11,那么你可能需要一个c风格的字符串。

mfile.open ( (accname + ".txt").c_str());