Jump to content
  • Advertisement
Sign in to follow this  
Plasmarobo

std::string to c-style string [solved, many thanks]

This topic is 4405 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

Now, normally I would just use std::string, but in this case I need to store to a char queue. This is my function right now:

//buffer = queue<char>
bool InputMan::scanline()
{
     
     char c[80];
     std::string m;
     getline(cin, m);

     //need to put m into c somehow (should I use .c_str()?
     //I tried it, but I couldn't figure it out
     //anyway, a method for storing the contents of m into c is needed!

     for(int i =0; i == 80; i++)
     {
             buffer.push(c);
     }
     
     return true;
} 


[Edited by - Plasmarobo on July 27, 2006 6:21:48 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by peous
yes, use
strcpy( buffer, myString.c_str() );


A more safe alternative would be (requires <cstring> and <string>):
std::string in = "test";
char out[80];
std::strncpy( out, in.c_str(), in.length()+1 );

Remember that both strncpy and strcpy is in namespace std.

Quote:
How DO you access the characters in an std::string object?

You either get a constant array of characters using string::c_str() or you access them invdividually with operator[].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!