...

View Full Version : how to print only N numbers of string



umen242
06-26-2008, 07:51 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 ?

Codman
06-26-2008, 02:31 PM
Hello umen242!

I am not able to understand your code. Can you be clearer to what you are doing? And also I want to add that you provide us the actual code which you have tried than we will help you further if you have any query or getting any error.

tomws
06-26-2008, 04:08 PM
recv() returns the number of bytes received, yes? Then set buf[rec_result+1]='\0';, and then just print buf. If you're trying to not modify the original buffer, you can make a second that is the same size and just copy the original contents into it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum