int to char C ++帮助我

时间:2013-10-20 01:52:31

标签: c++

它不起作用, 我需要保留许多文件,但名称不同, 例如: FILE1.TXT FILE2.TXT file3.txt 数字是计数器,它反击 是X。

感谢。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>


using namespace std;
using std::string;


string ItoStr (int x);

int main(){

   string cadena;
   int x = 1;
   ofstream fs("nombre" + ItoStr(x) + ".txt"); //Fail line
   fs << cadena;
   fs << cadena;
   fs.close();


    };


string ItoStr (int x){

    string str2;
    stringstream ss;
    ss << x;
    str2 = ss.str();
    return str2;

    };

1 个答案:

答案 0 :(得分:0)

你只需要分解它,然后改变:

ofstream fs("nombre" + ItoStr(x) + ".txt");

为:

string fname = "nombre" + ItoStr(x) + ".txt";
ofstream fs(fname.c_str());

问题是ofstream的构造函数(在C ++ 98中,您似乎正在使用)const char *,但您正在创建(临时)std::string