Click to See Complete Forum and Search --> : how to print only N numbers of string


umen
June 26th, 2008, 01:53 AM
Hello all
beginners question :
i have code that receives string buffer the buffer size is 128
but i like to print to console the string that actually receives , it can be 10 characters long or 5 or 20
i have :
...
int rec_result;

int lens = 128;

char buf[128];

rec_result = recv(new_fd,(char*)buf,lens,0);

// now i like to print only the buf string
// this code dos not work because i can't set the char array like this
// what is the alternative ?

int u = rec_result+1;

char bufprintout[6];

strncpy(bufprintout,buf,rec_result);

bufprintout[rec_result+1]='\0';

printf("server recv:%s",bufprintout);

what is the proper way to do that ?

souldog
June 26th, 2008, 02:54 AM
int rec_result(0);
int lens(128);
char buf[128] = {};

rec_result = recv(new_fd, (char*)buf, lens, 0);

std::cout << buf;