【问题标题】:C++ Probllem with output [closed]C ++输出问题[关闭]
【发布时间】:2014-08-08 02:03:14
【问题描述】:

我也需要用户输入他想要写入数据的文件。 示例:你想写什么文本文件“Hello Too”? 他键入: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();

【问题讨论】:

  • 字符串连接出现在任何好书的前几章中。
  • mfile.open (accname + ".txt");连接字符串

标签: c++ string random output


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您应该能够将字符串连接与+ 一起使用。

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

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

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

    【讨论】:

    • 得到这个错误:73 32 C:\Users\Winston\Desktop\C++\main.cpp [错误] 没有匹配函数调用 'std::basic_ofstream::open(std ::basic_string)'
    • @user3748896,查看更新。
    • 工作感谢 :D 我能做些什么来奖励你?
    • @user3748896,如果此答案解决了您的问题,请将其标记为已接受的答案。
    • 我不能等 5 分钟,但我会等到的。
    猜你喜欢
    • 2018-01-31
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2015-11-04
    相关资源
    最近更新 更多