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