Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to print only N numbers of string

    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 :
    ...
    Code:
    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 ?

  • #2
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •