想要创建许多不同的文件,但名称不同

时间:2012-07-09 15:45:16

标签: c++ string file file-io io

我需要创建一堆不同的文件,我想给它们命名如下:

"signal1.dat", "signal2.dat", "signal3.dat", ... , "signal100.dat"

这是我到目前为止所拥有的

std::stringstream ss;
std::string out = "signal";
std::string format = ".dat";
std::string finalName;
int TrkNxtSig = 1;

...
[code]  
... 


ss << out << TrkNxtSig << format;
finalName = ss.str();
ofstream output(finalName); 
TrkNxtSig++;

我收到此错误

     error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)’
/usr/include/c++/4.4/fstream:623: note: candidates are: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/fstream:608: note:                 std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:84: note:                 std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)

我不确定该错误消息告诉我的是什么。有什么问题?

1 个答案:

答案 0 :(得分:2)

它告诉你,ofstream没有以std::string为参数的构造函数。

改为使用ofstream output(finalName.c_str());

这已在C ++ 11中修复。如果你正在使用gcc,请尝试传递它-std=c++0x