Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,676 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,194 people online right now. Registration is fast and FREE... Join Now!




convert string to char[]

 
Reply to this topicStart new topic

convert string to char[]

aj32
post 31 Aug, 2007 - 03:06 PM
Post #1


D.I.C Addict

Group Icon
Joined: 30 Aug, 2007
Posts: 577



Thanked 2 times

Dream Kudos: 675
My Contributions


Hi, I am writing a C++ program that will create some files based on user input, I am using the X.append() function to append the file extension to the input, The only problem is that string will not work with ofstream and it needs to be converted to a char[].
In advance, Thanks for your help!
User is offlineProfile CardPM

Go to the top of the page

Bench
post 31 Aug, 2007 - 04:08 PM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


use the c_str() method to get the character array representation.

CODE
string filename = "myfile.txt";
ofstream ofs( filename.c_str() );
User is offlineProfile CardPM

Go to the top of the page

Pontus
post 2 Sep, 2007 - 11:44 AM
Post #3


Dreaming Coder / Coding Dreamer

Group Icon
Joined: 28 Dec, 2006
Posts: 527



Thanked 2 times

Dream Kudos: 275
My Contributions


u dont need to use append with strings, the following example will work fine.
CODE

{
string location="test";
location+=".txt";
}
User is offlineProfile CardPM

Go to the top of the page

aj32
post 4 Sep, 2007 - 11:47 AM
Post #4


D.I.C Addict

Group Icon
Joined: 30 Aug, 2007
Posts: 577



Thanked 2 times

Dream Kudos: 675
My Contributions


Thanks, Both of those functions work, I just found a another way, the strcat() function :

CODE
cout << strcat("STR", "STR");

or
CODE
ofstream FILE(strcat(userinput, ".ext"), ios::blablabla);

Thanks for your help! biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Bench
post 4 Sep, 2007 - 12:32 PM
Post #5


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


You were better off the way you had it before - strcat is a 'C' function. You should try to avoid using 'C' code when writing C++ (The two languages don't really mix very well)
User is offlineProfile CardPM

Go to the top of the page

aj32
post 5 Sep, 2007 - 03:45 AM
Post #6


D.I.C Addict

Group Icon
Joined: 30 Aug, 2007
Posts: 577



Thanked 2 times

Dream Kudos: 675
My Contributions


QUOTE(Bench @ 4 Sep, 2007 - 01:32 PM) *

You were better off the way you had it before - strcat is a 'C' function. You should try to avoid using 'C' code when writing C++ (The two languages don't really mix very well)


Ok, Thanks for the info!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:27AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month