現在一緒に作業している他の誰かから 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);