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?
whotookmyname
0
Newbie Poster
Recommended Answers
Jump to Postchange str to
str.c_str()
The function c_str() converts your std::string to aconst char *
which is what the constructor of fopen wants.
I personally likestd::ofstream(str.c_str())
better for opening files.
Jump to PostOne thing I noticed was that you did
string str = a + "x" + b+ ".txt"
It should bestring 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 …
All 5 Replies
Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster
CppFTW
21
Light Poster
ArkM
1,090
Postaholic
CppFTW
21
Light Poster
whotookmyname
0
Newbie Poster
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.