I'm using memcpy to copy a specific number of chars from a char array to a char *. But when I read the char * have always trash in the end.
I'm using libssh2 lib to send commands to my raspberry pi and receive the output.
libssh2_channel_read
will return the number of chars of the output int x
and the output text will be on the char buffer[32]
.
Code I'm using:
char buffer[32];
int x = libssh2_channel_read(channel, buffer, sizeof(buffer));
char * output = (char *)malloc(sizeof(char)*x);
memcpy(output, buffer, x-2); // x - 2 because of "\r\n"
libssh2_channel_free(channel);
channel = NULL;
cout << output << endl;
Example of output:
0══²²²²
I only want the 0