【问题标题】:How to create ofstream file with the name of (integer type) variable?如何使用(整数类型)变量的名称创建ofstream文件?
【发布时间】:2020-12-17 04:45:05
【问题描述】:
int a
cin>>a;
ofstream file;
file.open("a.txt");

我想用用户输入的代码创建一个 .txt 文件。请帮忙。

【问题讨论】:

  • 如果用户输入23,是否要打开23.txt?
  • yes 如果用户输入23,我要打开23.txt
  • 看:to_string.
  • 为什么不简单地将输入读取为字符串呢?为什么需要以整数形式读入? string name; cin >> name; file.open(name+”.txt”);

标签: c++ c++11 codeblocks ofstream


【解决方案1】:

需要将输入转换为std::string:

 int a;
 cin>>a;
 ofstream file(std::to_string(a) + ".txt");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多