[英]How to append a variable in to the output file's name in C++
我有一个C ++项目,可将数据输出到文件中。 以下是它的代码。
case 3:
{
outfile.open("price-change.csv");
if(! outfile)
{
cout << "Can not open outfile" << endl;
exit(1);
}
我想将文件名price-change-2014-12-23
为price-change-2014-12-23
,其中2014-12-23
是添加到文件名,价格更改中的变量。 有想法吗? 提前致谢。
std::string filename = "price-change-" + datestring + ".csv"
outfile.open(filename);
应该做。
Thomas Matthews在评论中指出,较早的编译器和不支持C ++ 11的编译器必须使用const char *
而不是std::string
打开文件。 如果以上内容产生错误消息并且无法使用C ++ 11,请使用
outfile.open(filename.c_str());
而且,如果您涉及整数或浮点数之类的数字,则可以使用stringstream轻松构建日期字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.