943,739 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1127
  • C++ RSS
Jun 15th, 2009
0

how to convert variable string into cons char

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whotookmyname is offline Offline
9 posts
since Jun 2009
Jun 15th, 2009
0

Re: how to convert variable string into cons char

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.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 15th, 2009
0

Re: how to convert variable string into cons char

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.
Reputation Points: 31
Solved Threads: 1
Light Poster
CppFTW is offline Offline
41 posts
since Dec 2008
Jun 15th, 2009
0

Re: how to convert variable string into cons char

>...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
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 15th, 2009
0

Re: how to convert variable string into cons char

Oh, yeah... Just was confused from when he wrote "axb.txt"
Nevermind, the first part of my previous post
Reputation Points: 31
Solved Threads: 1
Light Poster
CppFTW is offline Offline
41 posts
since Dec 2008
Jul 6th, 2009
0

Re: how to convert variable string into cons char

thank you very much guys
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whotookmyname is offline Offline
9 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: displaying the results
Next Thread in C++ Forum Timeline: Create GUI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC