0

I have to open a file named "axb.txt"
I have user input a and b as strings
i have concatenated the string using string str = a + "x" + b+ ".txt"
now i need to open the file.
it does not open with fopen(str, ios::app)
because str is not a const char pointer.
what to do?

4
Contributors
5
Replies
8
Views
8 Years
Discussion Span
Last Post by whotookmyname
0

change str to str.c_str() The function c_str() converts your std::string to a const char * which is what the constructor of fopen wants.
I personally like std::ofstream(str.c_str()) better for opening files.

0

One thing I noticed was that you did string str = a + "x" + b+ ".txt" It should be string str = "a" + "x" + "b" + ".txt" because if you just put a and b without quotes, the compiler will think that you are using the variables a and b.

To answer, your question, yeah just use str.c_str() like niek_e said.

0

>...because if you just put a and b without quotes, the compiler will think that you are using the variables a and b.
That's exactly what OP intends to do ;)

0

Oh, yeah... Just was confused from when he wrote "axb.txt"
Nevermind, the first part of my previous post :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.