[英]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.