打开带有输入名称的文件

时间:2012-11-01 15:33:53

标签: c++ string file-io stringstream

我在C ++中从我正在使用的其他人那里获得了这段代码,但我不确定为什么添加了“std :: string()”。

std::ifstream File;
std::stringstream FileName;
FileName << Name; //Name being a string that has been passed as an input to the function.
                 // Eg."MyFile"
newFileName << ".txt"; //"MyFile.txt"

File.open(std::string(FileName.str()).c_str(), std::ios::in | std::ios::binary);

我的问题是,因为str()返回一个字符串,并且c_str()获取一个字符串并将其转换为c字符串,为什么我们需要将它放在“string()”中呢?难道不能写得像:

File.open((FileName.str()).c_str(), std::ios::in | std::ios::binary);

1 个答案:

答案 0 :(得分:1)

是的,它可以这样写。

使用

std::string(FileName.str())

绝对没有意义。

相关问题