2

I am getting an error when I run this piece of code

  string line;
  getline (myfile,line);
  char file_input[15];
  strncpy(file_input, line, 6);
  cout << line << endl;

Error - cannot convert ‘std::string’ to ‘const char*’ for argument ‘2’ to ‘char* strncpy(char*, const char*, size_t)’ How to get rid of this?

MSalters
  • 173,980
  • 10
  • 155
  • 350
Vishal
  • 989
  • 2
  • 11
  • 19
  • 4
    What do you want to achieve here? While you can get rid of this error rather easily, there’s probably a better alternative than using a fixed-sized buffer to begin with. – Konrad Rudolph Jul 04 '11 at 16:13
  • possible duplicate of [Convert std::string to const char* or char*](http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char) – Benjamin Lindley Jul 04 '11 at 16:15

3 Answers3

5

Try :

strncpy(file_input, line.c_str(), 6);

This uses the c_str() member function of the std::string type.

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • DO NOT DO THIS! This is undefined behavior. The prototype for basic_string::c_str() is `const charT* c_str() const;`. The standard goes on to say " The program shall not alter any of the values stored in the array. Nor shall the program treat the returned value as a valid pointer value after any subsequent call to a non-const member function of the class basic_string that designates the same object as `this`." – David Hammen Jul 04 '11 at 16:24
  • @David: Sander is copying *from* `line.c_str()`, not into it. `strncpy`'s argument order reflects the assignment operator's. – Fred Foo Jul 04 '11 at 16:25
  • Ignore my the previous comment. I read it exactly backwards. This is a perfectly valid use of c_str(). – David Hammen Jul 04 '11 at 16:34
3
strncpy(file_input, line.c_str(), 6);

But why would you want the fixed-length buffer file_input in the first place? It would be safer to construct it as a new std::string containing the first five characters in line:

std::string file_input(line, 0, 5);
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • I want to perform some operations like substring, string comparison etc with the string input taken from file. – Vishal Jul 04 '11 at 16:17
  • @Vishal: then use `std::string`, which has all these operations and makes them a lot easier. http://www.cplusplus.com/reference/string/string/ – Fred Foo Jul 04 '11 at 16:18
  • If you're going to use a lot of `substring`, I suggest using `std::string_view` instead, as it will not build copies, so it will be really good for performance. – A. Smoliak Oct 27 '21 at 18:21
0

Converting from c++ std string to C style string is really easy now.

For that we have string::copy function which will easily convert std string to C style string. reference

string::copy functions parameters serially

  1. char string pointer
  2. string size, how many characters will b copied
  3. position, from where character copy will start

Another important thing,

This function does not append a null character at the end of operation. So, we need to put it manually.

Code exam are in below -

// char string
char chText[20];

// c++ string
string text =  "I am a Programmer";

// conversion from c++ string to char string
// this function does not append a null character at the end of operation
text.copy(chText, text.size(), 0);

// we need to put it manually
chText[text.size()] = '\0';

// below statement prints "I am a Programmer"
cout << chText << endl;
SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44