如何获取以2个用户输入命名的txt文件

时间:2017-11-13 17:44:25

标签: c++

我每个月创建的txt文件必须采用以下格式:" expense_month_year.txt"。
另外,我无法将输出文件变为txt文件 请建议

using namespace std;

int main() {

    string month;
    int year;

    ofstream file;

    cout << "Please enter the month." << endl;
    getline(cin,month);

    cout << "Please enter the year." << endl;
    cin >> year;

    //Example of txt file has to be named as follow "expenses_January_2017"
    file.open(month.c_str() );
    file.close();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

使用std::stringstream

#include <sstream>


string month
int year;

stringstream filename;

cin >> month;
cin >> year;

filename << "expenses_" << month << "_" << year;
ofstream file(filename.str());

// . . .