clearing a char* ??

This is a discussion on clearing a char* ?? within the C++ Programming forums, part of the General Programming Boards category; I have a buffer called sendbuf. I am using it in a winsock program. It is set to the text ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    11

    clearing a char* ??

    I have a buffer called sendbuf. I am using it in a winsock program. It is set to the text the user entered, and then sent to the server. The problem is, that if a user types in "test" and then "a" the output would be "aest". I need to clear the sendbuf array so that it doesn't have leftover characters from the last input, but i cant figure out how!

    sendbuf = ""; gets errors.

    help would be nice


    Thx in advance,
    Glorfindel

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    strcpy( sendbuf, "" );
    or, if you know the size of sendbuf,
    Code:
    memset( sendbuf, '\0', size_of_sendbuf );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    hummm... perhaps the buffer being cleared isn't the issue. I have tried using those to clear it, but the output still will be "aest" with input of "test" and "a"...
    Code:
    char *sendbuf = new char[32];
    while(strcmpi(sendbuf, "quit")) {
        strcpy( sendbuf, "" );
        cout<<"input:";
        cin.getline(sendbuf, 32, '\n');
        send( m_socket, (char*)sendbuf, strlen(sendbuf), 0 );
    }
    Thats the code. If sendbuf != to "quit" it should input new data, and send it to the server.
    Last edited by Glorfindel; 01-05-2004 at 07:56 PM.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    11


    The problem was that I was forgetting to clear the buffer on the server too!

    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Clearing Terminal
    By mrginger in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 11:58 AM
  2. Problems Clearing Portion of Screen
    By Peter5897 in forum Windows Programming
    Replies: 2
    Last Post: 05-29-2006, 03:23 AM
  3. clearing cstring
    By curlious in forum C Programming
    Replies: 4
    Last Post: 11-30-2004, 09:30 AM
  4. clearing things up
    By webturtle0 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-28-2002, 02:59 PM
  5. Clearing the screen
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 05-23-2002, 01:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21