CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2006
    Posts
    230

    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 :
    ...
    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
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: how to print only N numbers of string

    Code:
    int rec_result(0);
    int lens(128);
    char buf[128] = {};
    
    rec_result = recv(new_fd, (char*)buf, lens, 0);
    
    std::cout << buf;
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured