Memcpy, string and terminator

I need to write a function that fills the char * buffer for the designated length with the contents of the string. If the string is too long, I just need to cut it. The buffer is not allocated by me, but by the user of my function. I tried something like this:

int writebuff(char* buffer, int length){
    string text="123456789012345";
    memcpy(buffer, text.c_str(),length);
    //buffer[length]='\0';
    return 1;
}


int main(){
    char* buffer = new char[10];
    writebuff(buffer,10);
    cout << "After: "<<buffer<<endl;
}

My question is about the terminator: should it be there or not? This function is used in a much wider code, and sometimes it seems that I am having problems with strange characters when a string needs to be cut.

Any hints of the right procedure to follow?

+5
source share
10 answers

A C style string must end with a null character '\0'.

, - - . undefined. , , , . .

P.S. . Naveen , . , , , , <= 0.

int writebuff(char* buffer, int length)
{
    string text="123456789012345";
    if (length <= 0)
        return text.size();
    if (text.size() < length)
    {
        memcpy(buffer, text.c_str(), text.size()+1);
        return text.size();
    }
    memcpy(buffer, text.c_str(), length-1);
    buffer[length-1]='\0';
    return length-1;
}
+6

, NULL . length-1 memcpy length-1 \0.

+6

, ++ - , ( , NUL)

int writebuff(char* buffer, int length)
{
  string text = "123456789012345";
  std::fill_n(buffer, length, 0); // reset the entire buffer
  // use the built-in copy method from std::string, it will decide what best.
  text.copy(buffer, length);
  // only over-write the last character if source is greater than length
  if (length < text.size())
    buffer[length-1] = 0;
  return 1; // eh?
}
+2

char * , , .

+1

*, , . imo, strncpy memcpy, . ( ).

* - , !

0

: ?

. . , , ? cout? , , \0. .

, . , . main(), ; , , , , . , ( ) .

0

, \0, writebuff. , buffer, C- , \0.

, c_str() \0 , text.size() + 1 . , length , , text ( min(length - 2, text.size() + 1/*trailing \0*/), , buffer[length - 1] = 0, ).

buffer, main, , btw

0

Necrolis , strncpy - , , . , , , . ( C, , , C, ++?)

int writebuff(char* buffer, int length){
    char* text="123456789012345";
    strncpy(buffer, text, length);
    buffer[length-1]='\0';
   return 1;
}
0

-, , writerbuff . , , , writebuff .

-, , . -, operator<<(ostream, char*). -, . undefined.

( - - , length text?)

:

int writebuff(char* buffer, int length){
  string text="123456789012345";
  memcpy(buffer, text.c_str(),length);
  buffer[length-1]='\0';
  return 1;
}


int main(){
  char* buffer = new char[10];
  writebuff(buffer,10);
  cout << "After: "<<buffer<<endl;
}
0
  • main() delete , new., (char buf[10]). , 10 , , "" , , , , . .

  • In C / C ++, a common contract with character buffers is that they have a zero end, so I would have included it if I had not been explicitly declared not to do this. And if I did, I would comment on it and maybe even use typedef or name in the parameter char *, indicating that the result is a string that is not terminated by zero.

0
source

All Articles