Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

how to convert from char * to string?

string

  • Please log in to reply
7 replies to this topic

#1 alirezan

alirezan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 74 posts

Posted 19 September 2011 - 02:26 PM

Hi
I am trying to find a way to convert from char * to string.
I have:

char * buf = "test";

and I want to look for a substring in this. I tried using strcpy:

string s;
strcpy (s, buf);

but it keeps telling me: "No instance of function "strcpy" matches the argument list.

Anybody knows a way of doing this?

Thanks
  • 0

#2 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 19 September 2011 - 03:50 PM

IDK about that error, but first I must tell you, strcpy is NOT compatible with C++ strings. If you want to convert from char * to string, you would do something like this:
char *but = "test";
string s;
s = buf;
If you want to use strcpy, however, for copying one C string to another, you must make sure you have the library included:
#include <cstring> //For C++
#include <string.h> //For C

BTW, use the code tags for you code. In the editor, it is the # symbol.
  • 0
Latinamne loqueris?

#3 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 19 September 2011 - 04:57 PM

You can simply use the (overloaded) assignment operator to assign a char * to a std::string. This will copy all the data over automatically.

If you're trying to copy from a std::string to a char *, the best way to do that is with strncpy(), but you have to get the char data from the string first!

size_t my_str_size_with_null_end = my_str->size() + 1;
  char *buffer = malloc(my_str_size_with_null_end);
  strncpy(buffer, my_str->c_str(), my_str_size_with_null_end);

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 19 September 2011 - 05:01 PM

...

char *but = "test";
string s;
s = buf;
...

In 'char * but= "test";' , I suppose you meant 'char * buf= "test";' .


...In the editor, it is the # symbol.

This editor? I thought it was [CODE] and [/CODE] that make code tags.
  • 0

#5 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 19 September 2011 - 05:37 PM

@RhetoricalRuvim: Both of them work. I prefer [noparse][code=auto:0][/noparse] tags myself, along with any other tags I can use. The only real trouble I have with the quick reply window here is that it keeps the formatting of pasted text, which forces me to put it through a text editor first. -_-
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 19 September 2011 - 06:15 PM

LOL typo, sorry.
  • 0
Latinamne loqueris?

#7 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 20 September 2011 - 06:22 AM

No one really mentioned the std::string ctor:
char* buf = "test";
std::string s(buf);

  • 0

The roots of education are bitter, but the fruit is sweet.


#8 Biofag

Biofag

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 29 October 2012 - 12:43 AM

don't forget the size, if you don't specify the buf size in the constructor, you could read garbage:

std::string s(buf, buf_size);

I'd use this, i had problems reading input files in a game and that was the cause
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download