Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a C++ Expert!

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



convert string to char[] Rate Topic: -----

#1 aj32  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 17
  • View blog
  • Posts: 577
  • Joined: 30-August 07


Dream Kudos: 675

Share |

convert string to char[]

Post icon  Posted 31 August 2007 - 03:06 PM

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!
Was This Post Helpful? 0
  • +
  • -


#2 Bench  Icon User is offline

  • D.I.C Lover
  • Icon

Reputation: 304
  • View blog
  • Posts: 1,684
  • Joined: 20-August 07


Dream Kudos: 200

Expert In: C/C++

Re: convert string to char[]

Posted 31 August 2007 - 04:08 PM

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

 string filename = "myfile.txt";
 ofstream ofs( filename.c_str() ); 

Was This Post Helpful? 1

#3 Pontus  Icon User is offline

  • Dreaming Coder / Coding Dreamer
  • Icon

Reputation: 9
  • View blog
  • Posts: 586
  • Joined: 28-December 06


Dream Kudos: 275

Re: convert string to char[]

Posted 02 September 2007 - 11:44 AM

u dont need to use append with strings, the following example will work fine.
{
 string location="test";
 location+=".txt";
}


Was This Post Helpful? 0
  • +
  • -

#4 aj32  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 17
  • View blog
  • Posts: 577
  • Joined: 30-August 07


Dream Kudos: 675

Re: convert string to char[]

Posted 04 September 2007 - 11:47 AM

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

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

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


Thanks for your help! :D
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • Icon

Reputation: 304
  • View blog
  • Posts: 1,684
  • Joined: 20-August 07


Dream Kudos: 200

Expert In: C/C++

Re: convert string to char[]

Posted 04 September 2007 - 12: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)
Was This Post Helpful? 0
  • +
  • -

#6 aj32  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 17
  • View blog
  • Posts: 577
  • Joined: 30-August 07


Dream Kudos: 675

Re: convert string to char[]

Posted 05 September 2007 - 03:45 AM

View PostBench, on 4 Sep, 2007 - 01:32 PM, said:

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!
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users