Thread: how to print only N numbers of string

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    58

    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. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You could make buf to contain one extra char, and then just set a '\0' at location buf[rec_result] (provided rec_result is not signifying an error).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM