用输入名称打开文件 我从我正在工作的其他人那里得到了C++代码,但是我不知道为什么要添加“STD::Strug()”。p> 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);

用输入名称打开文件 我从我正在工作的其他人那里得到了C++代码,但是我不知道为什么要添加“STD::Strug()”。p> 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);,c++,string,file-io,stringstream,C++,String,File Io,Stringstream,是的,可以这样写 使用 是毫无意义的。所以我写的很好,即使我的文件中没有“using namespace std;”,对吧?谢谢你的快速回答@Mulan-是的,这很好,因为在这种情况下,toy不需要访问此名称,只需使用.str()即可。std::string()创建另一个临时字符串对象,这是不必要的。 File.open((FileName.str()).c_str(), std::ios::in | std::ios::binary); std::string(FileName.str())

是的,可以这样写

使用


是毫无意义的。

所以我写的很好,即使我的文件中没有“using namespace std;”,对吧?谢谢你的快速回答@Mulan-是的,这很好,因为在这种情况下,toy不需要访问此名称,只需使用
.str()
即可。std::string()创建另一个临时字符串对象,这是不必要的。
File.open((FileName.str()).c_str(), std::ios::in | std::ios::binary);
std::string(FileName.str())