how to convert variable string into cons char

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 9
Reputation: whotookmyname is an unknown quantity at this point 
Solved Threads: 0
whotookmyname whotookmyname is offline Offline
Newbie Poster

how to convert variable string into cons char

 
0
  #1
Jun 15th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,828
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: how to convert variable string into cons char

 
0
  #2
Jun 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: CppFTW is an unknown quantity at this point 
Solved Threads: 0
CppFTW CppFTW is offline Offline
Newbie Poster

Re: how to convert variable string into cons char

 
0
  #3
Jun 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to convert variable string into cons char

 
0
  #4
Jun 15th, 2009
>...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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: CppFTW is an unknown quantity at this point 
Solved Threads: 0
CppFTW CppFTW is offline Offline
Newbie Poster

Re: how to convert variable string into cons char

 
0
  #5
Jun 15th, 2009
Oh, yeah... Just was confused from when he wrote "axb.txt"
Nevermind, the first part of my previous post
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 9
Reputation: whotookmyname is an unknown quantity at this point 
Solved Threads: 0
whotookmyname whotookmyname is offline Offline
Newbie Poster

Re: how to convert variable string into cons char

 
0
  #6
Jul 6th, 2009
thank you very much guys
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC