Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

johnnyBravo

How to convert std::string to char array?

This topic is 5453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say i''ve got: char cString[100]; std::string sString; sString="Hellow World"; then i want: cString = sString; Basically i want to convert the std::string to a char. But i dont know how to do this...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
strncpy(cString, sString.c_str(), 99);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Deficite
Haven''t tested, but this should work:
strcpy(cString, sString.c_str());

That would work, but mine was better. If a C string manip routine doesn''t have an "n" in the name, don''t use it.

Share this post


Link to post
Share on other sites
its compiles and all and doesnt crash, but it seems to return nothing...im opening a file and reading a line with fstream, maybe im not reading the file properly.

Also im displaying it with dx so thats y i need the char...

Share this post


Link to post
Share on other sites
i tried that i got the error
''='' : cannot convert from ''const char *'' to ''char [44]''

anyway the other examples work..the i wasnt reading the files properly...

thanks

Share this post


Link to post
Share on other sites
quote:
Original post by rpg_code_master
is it not cString = sString.data( );
"Returns a pointer to an array of characters (not necessarily null-terminated) representing the string''s contents."

C strings must be null-terminated. And that wouldn''t make a copy but only point to sString''s internals.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by johnnyBravo
its compiles and all and doesnt crash, but it seems to return nothing...im opening a file and reading a line with fstream, maybe im not reading the file properly.

Maybe not, but how would we know? Show us the code.

Or even better: Learn how to use the debugger. Seriously. It will save you huge amounts of pain, frustration and time.

Share this post


Link to post
Share on other sites
oh no dont worry, it was, i ended up copying a fstream example and it worked, im not sure what i did, but ahwell!

Yeah recently i worked out in vc6++ u can double click on errors and it goes to it!

[edited by - johnnyBravo on October 21, 2003 5:28:48 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!