我希望输出文件与输入文件具有相同的名称(具有不同的扩展名)例如:输入:packet_a.raw,输出:packet_a_data.txt
我尝试将文件名保存在字符串中,但 ifstream 和 ofstream 不接受字符串。我尝试使用 char[] 但是我很难修改它。
您可以使用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();
}
这就是图书馆得到回报的地方。查看 Boost文件系统库,或者您的 IDE 的平台库(例如,MFC)。