Mulan
Mulan

Reputation: 31

Open file with input name

I've got this piece of code in C++ from someone else that I am now working with but I'm not sure why the "std::string()" is been added.

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);

My question is, since str() returns a string, and c_str() gets a string and transforms it into c string, why do we need to put it inside the "string()"? Could it not be writen like:

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

Upvotes: 2

Views: 1242

Answers (1)

Kiril Kirov
Kiril Kirov

Reputation: 38163

Yes, it can be written like this.

Using

std::string(FileName.str())

is absolutely meaningless.

Upvotes: 1

Related Questions