Jonathan Prior
Jonathan Prior

Reputation: 6294

convert a char* to std::string

I need to use an std::string to store data retrieved by fgets(). To do this I need to convert the char* return value from fgets() into an std::string to store in an array. How can this be done?

Upvotes: 370

Views: 907509

Answers (13)

Jesse Beder
Jesse Beder

Reputation: 34054

std::string has a constructor for this (see constructor (7)):

const char *s = "Hello, World!";
std::string str(s);

Note that this constructor deep-copies the character list at s, and s should not be nullptr; otherwise, the behavior is undefined.

Upvotes: 526

Erik van Velzen
Erik van Velzen

Reputation: 7062

I would like to mention a new method which uses the user defined literal s. This isn't new, but it will be more common because it was added in the C++14 Standard Library.

Largely superfluous in the general case:

using namespace std::literals;
 
string mystring = "your string here"s;

But it allows you to use auto, also with wide strings:

auto mystring = U"your UTF-32 string here"s;

And here is where it really shines:

string suffix;
cin >> suffix;
string mystring = "mystring"s + suffix;

Upvotes: 8

payam purchi
payam purchi

Reputation: 276

This question turns up in results for how to convert char.

char c1 = 'z';
char c2 = 'w';
string s1{c1};
string s12{c1, c2};

You can do something similar for char*:

const char* c3 = "z";
string s3{c3};

Upvotes: 4

SHAH MD IMRAN HOSSAIN
SHAH MD IMRAN HOSSAIN

Reputation: 2899

Converting from C style string to C++ std string is easier

There is three ways we can convert from C style string to C++ std string

First one is using constructor,

char chText[20] = "I am a Programmer";
// using constructor
string text(chText);

Second one is using string::assign method

// char string
char chText[20] = "I am a Programmer";

// c++ string
string text;

// convertion from char string to c++ string
// using assign function
text.assign(chText);

Third one is assignment operator(=), in which string class uses operator overloading

// char string
char chText[20] = "I am a Programmer";

// c++ string
// convertion from char string to c++ string using assignment operator overloading
string text = chText;

third one can be also write like below -

// char string
char chText[20] = "I am a Programmer";

// c++ string
string text;


// convertion from char string to c++ string
text = chText;

Third one is little straight forward and can be used in both situation

  1. while we are declaring and initializing
  2. while we are assigning multiple times after object creation or initialization

Upvotes: 8

James Thompson
James Thompson

Reputation: 48212

Pass it in through the constructor:

const char* dat = "my string!";
std::string my_string( dat );

You can use the function string.c_str() to go the other way:

std::string my_string("testing!");
const char* dat = my_string.c_str();

Upvotes: 22

Atul
Atul

Reputation: 4340

Most answers talks about constructing std::string.

If already constructed, just use assignment operator.

std::string oString;
char* pStr;

... // Here allocate and get character string (e.g. using fgets as you mentioned)

oString = pStr; // This is it! It copies contents from pStr to oString

Upvotes: 61

user1564286
user1564286

Reputation: 325

I've just been struggling with MSVC2005 to use the std::string(char*) constructor just like the top-rated answer. As I see this variant listed as #4 on always-trusted http://en.cppreference.com/w/cpp/string/basic_string/basic_string , I figure even an old compiler offers this.

It has taken me so long to realize that this constructor absolute refuses to match with (unsigned char*) as an argument ! I got these incomprehensible error messages about failure to match with std::string argument type, which was definitely not what I was aiming for. Just casting the argument with std::string((char*)ucharPtr) solved my problem... duh !

Upvotes: 6

Presen
Presen

Reputation: 1867

char* data;
stringstream myStreamString;
myStreamString << data;
string myString = myStreamString.str();
cout << myString << endl;

Upvotes: 10

Vern Jensen
Vern Jensen

Reputation: 3570

Not sure why no one besides Erik mentioned this, but according to this page, the assignment operator works just fine. No need to use a constructor, .assign(), or .append().

std::string mystring;
mystring = "This is a test!";   // Assign C string to std:string directly
std::cout << mystring << '\n';

Upvotes: 1

user1629342
user1629342

Reputation:

const char* charPointer = "Hello, World!\n";
std::string strFromChar;
strFromChar.append(charPointer);
std::cout<<strFromChar<<std::endl;

Upvotes: 12

Eugene
Eugene

Reputation: 7258

If you already know size of the char*, use this instead

char* data = ...;
int size = ...;
std::string myString(data, size);

This doesn't use strlen.

EDIT: If string variable already exists, use assign():

std::string myString;
char* data = ...;
int size = ...;
myString.assign(data, size);

Upvotes: 176

Paul
Paul

Reputation: 13238

I need to use std::string to store data retrieved by fgets().

Why using fgets() when you are programming C++? Why not std::getline()?

Upvotes: 32

Daniel A. White
Daniel A. White

Reputation: 191037

char* data;
std::string myString(data);

Upvotes: 2

Related Questions