2013-04-27 200 views
0

我想輸出文件具有相同的名稱作爲輸入文件(用不同的擴展名) 例如:輸入:packet_a.raw,輸出:packet_a_data.txt輸入/輸出文件名

我試圖保存的文件名在一個字符串中,但是ifstream和ofstream不接受一個字符串。 我嘗試使用char [],但後來我很難修改它。

+0

'std :: string'有'c_str()'方法,它返回字符串內容的'char []'。但是,請注意,您**不得**修改'c_str()'返回的數組內容。 – 2013-04-27 20:28:51

回答

0

這是圖書館的回報。查看Boost filesystem library或您的IDE平臺庫(例如MFC)。

0

您可以使用char[]然後修改它sprintf。類似的東西:

ofstream outFile_raw; 
ofstream outFile_txt; 
std::string data; 

std::string format_raw="raw"; 
std::string format_txt="txt"; 

char fileName[20]; 
sprintf(fileName, "..\\Packages\\packet_a.%s", format_raw); 

outFile_raw.open(fileName, ios::trunc); 

if(outFile_raw.is_open()) 
{ 
    outFile_raw<< data; // Write contents of the data to the file stream. 
    outFile_raw.close(); 
} 

sprintf(fileName, "..\\Packages\\packet_a.%s", format_txt); 

outFile_txt.open(fileName, ios::trunc); 

if(outFile_txt.is_open()) 
{ 
    outFile_txt<< data; // Write contents of the data to the file stream. 
    outFile_txt.close(); 
}