Removing junk from end of string

This is a discussion on Removing junk from end of string within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I've been working a lot with character arrays lately, but I cant seem to get something right. When ...

  1. #1
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428

    Removing junk from end of string

    Hello everyone. I've been working a lot with character arrays lately, but I cant seem to get something right. When I allocate some memory for a string (char *string=new char[256]), it will fill it up with random characters. Then, when I copy part of another string into that one, it will copy over, but it still leaves all of the random junk at the end of the string. I've tried setting the character after the copied string in 'string' to 'NULL' and '\0', but it still just left all the junk in there. So, is there something im missing with this, ive been using character arrays for some time, but just recently ran into this problem. Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Before working on the string, memset it to nuls:

    memset ( array, SIZE, '\0' );

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Thanks for the reply. I tried that, but it didnt seem to do anything, heres some source:

    char *var=new char[256];
    memset (var, 256, '\0' );
    cout<<"VARALLOC:"<<var<<endl;

    It still prints out all of the garbage, do you know what im doing wrong? Thanks.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Check the prototype for memset.

  5. #5
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Just did , Well, that fixed it (had to switch the 256 and the '\0'), thanks!

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    63
    you could also make a loop in order to set each character of the array to NULL.

    I do often, I think it improves the understanding of loops and character pointers.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  7. #7
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    I had also tried the loop, but it didnt seem to work before:

    for(i=0;i<256;i++)
    {
    string[i]=NULL //I also had it string[i]='\0', but it didnt seem to work either
    }

    I dont know why it wouldnt, thought it was pretty strange.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    63
    try using a character pointer and instatiating it to NULL or '\0'
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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