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?

Recommended Answers

All 5 Replies

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.

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.