Extra characters with

This is a discussion on Extra characters with within the C Programming forums, part of the General Programming Boards category; When I use strncat, I get strange characters appended to my string, although the string buffer is only 8 bytes ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    28

    Extra characters with

    When I use strncat, I get strange characters appended to my string, although the string buffer is only 8 bytes in size which is verified and its told to only copy 8 characters (which I've even tried hardcoding). Where are they coming from?

    Code:
    RecvMsg(buf, client, sizeof(buf)); //Recieve our packet
    char p[((uint8(buf[0])<<8)|uint8(buf[1]))]; //Allocate just enough to hold the playername
    memset(p, 0, sizeof(p));
    std::string playerName = strncpy(p, buf + 2, sizeof(p));
    Then std::cout << "Playername: " << p << std::endl;
    or std::cout << "Playername:" << playerName << std::endl;
    or printf(p);
    or printf(playerName.c_str());

    all result in:

    MasterCQ��t�O��t�

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all this is not C, but C++.

    Secondly, just looking over your code I think you are confusing the use of sizeof with the use of strlen.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you forget to include the end of string char: '\0'?

    Does your string have a newline char: '\n' just before the end of it?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    claudiu, not only was your post non-educational, it assumed incorrectly and seems to describe someone who can't identify standard C functions. The example given is 90% C and could easily be entirely C.

    sizeof() returns the size of the array in bytes, which in this case (and because char is normally 1 byte), returns the same thing. strlen would be more accurate to use but I can't use strlen because it uses a null character to determine the end of the string, which in this case, doesn't work.

    The string wasn't null terminated. Adding a '\0' to the end of the c string fixed it or
    Code:
    std::string playerName(strncpy(p, buf + 2, sizeof(p)), sizeof(p));
    or
    Code:
    std::string playerName = strncpy(p, buf+2, sizeof(p)+1);
    to get the string to add one for me. Thank you for noticing my mistake.
    Last edited by computerquip; 09-29-2010 at 02:32 PM.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Couldn't you accomplish the same thing with this code and skip the intermediary buffer and temporaries?

    Code:
    RecvMsg(buf, client, sizeof(buf)); //Recieve our packet
    std::string playerName(buf + 2, ((uint8(buf[0])<<8)|uint8(buf[1])));
    Untested, but just a thought.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange characters in output to terminal
    By hellogamesmaste in forum C Programming
    Replies: 4
    Last Post: 08-30-2009, 10:25 AM
  2. Extra characters in string
    By Tigers! in forum C Programming
    Replies: 10
    Last Post: 07-28-2009, 11:48 PM
  3. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  4. problem with reading characters
    By csvraju in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:59 AM
  5. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM

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