c_str() truncating string (randomly?)

This is a discussion on c_str() truncating string (randomly?) within the C++ Programming forums, part of the General Programming Boards category; Hi, Im currently trying to write a program to model the "Weasel" evolution model, described here: Weasel program - Wikipedia, ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    c_str() truncating string (randomly?)

    Hi, Im currently trying to write a program to model the "Weasel" evolution model, described here: Weasel program - Wikipedia, the free encyclopedia

    This basically consists of setting a target phrase, then, starting from a completely random phrase, "evolving" that random phrase towards the target phrase
    The program itself seem to work fine, but I'm having a bit of difficulty with my output. Im using FLTK for the GUI, which cant handle normal strings, but requires converting them via
    Code:
    exmplstr.c_str()
    Im outputting a new phrase, in the form of a c-string every 0.01 seconds by appending it to a buffer, which is then output in a text box.
    My problem is that, seemingly randomly, when I use c_str(), the original string will become truncated, but this only ever happens where there is a space (not always the first space either):

    Str: "1 2 3 4 5 6 7 8 9 0 { }...etc" <-multiple spaces in string
    C-str "1 2 3 4 5"
    Since this seems to happen seemingly randomly, I am at a loss as to what it might be, and google wasn't much help!

    The particular code that converts the strings and adds them to the buffer is here:

    Code:
    cout << "\nGen Num: " << gn;
    e1.setphrases(e1.breed()); //Generates new phrases string o = "\n Best from generation "; o.append(intstr(gn)); //Append Number of generations thus far o.append(" is: "); cout << e1.getHiScr(); //Get random phrase closest to target phrase o.append(e1.getHiScr()); //Append this to o cout << "\nNrm: " << o; //Testing difference cout << "\n Cstr: " << o.c_str(); //between original and converted progress->value(e1.getScr(e1.getHiScr())); //Update progress bar phrsbuff->append(o.c_str()); //Convert string o created above to cstr, append to text buffer outputbox->buffer(phrsbuff); //Put buffer in output box outputbox->redraw(); //Refresh box outputbox->insert_position(outputbox->buffer()->length()); //Scroll output outputbox->show_insert_position(); //box down Fl::add_timeout(0.01, generate_cb);
    Sorry to dump a big wall of text. Ill attach all the code, as its easier to see what I mean if you see the program running. Also, I'm using Quincy: Simple free C/C++ programming IDE for Windows as an editor and compiler
    Thanks

    Chris
    Attached Files Attached Files
    Last edited by chris2306; 01-09-2011 at 03:28 PM. Reason: Add quincy and attatchment

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't have a 0-byte in a string, since that marks the end of your string. (Not the character '0', but the character '\0', i.e., with ascii value 0.) If your genetic mutation thing can create a string with a 0-byte in the middle, then you'll have to fix it.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    Quote Originally Posted by tabstop View Post
    You can't have a 0-byte in a string, since that marks the end of your string. (Not the character '0', but the character '\0', i.e., with ascii value 0.) If your genetic mutation thing can create a string with a 0-byte in the middle, then you'll have to fix it.
    Could you possibly elaborate on that please? The example I gave of the difference between the normal string and the converted string was pretty literal, I'm not sure why one space in the string would be case it to truncate and not one of the other spaces?

    Ive been using " 1 2 3 4 5 6 7 8 9 0 - = [ ] ; ' # , . /" as a test target phrase, and the most recent run I did gave:

    Normal String: ",1 2 3m4 5 6 7 8 q X - = [ ].C ' # f . /"
    Converted: ",1 2 3m4 5 6 7 8 q X -"

    Thanks for the quick reply

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If it was a space, you wouldn't have a problem. I'm guessing you have an actual \0 character, which is not at all like a space, really.

    Are you still doing those debug print statements I see posted here? Is o short, or just o.c_str? If your strings are always supposed to be the same length, you could do
    Code:
    for (int k = 0; k < string_length; k++) {
        cout << static_cast<int> o[k] << " ";
    }
    to see the actual values of the characters.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    Quote Originally Posted by tabstop View Post
    If it was a space, you wouldn't have a problem. I'm guessing you have an actual \0 character, which is not at all like a space, really.

    Are you still doing those debug print statements I see posted here? Is o short, or just o.c_str? If your strings are always supposed to be the same length, you could do
    Code:
    for (int k = 0; k < string_length; k++) {
        cout << static_cast<int> o[k] << " ";
    }
    to see the actual values of the characters.
    Yep I am, that was where I got these from:

    Normal String: ",1 2 3m4 5 6 7 8 q X - = [ ].C ' # f . /"
    Converted: ",1 2 3m4 5 6 7 8 q X -"
    Thanks for the help, I will try what you suggest and see what I get

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Thumbs up

    Thanks so much you were right, turns out the range of random numbers I was generating was one larger than my character dictionary, so every now and then it would reach outside it and get that \0 value

    Thanks again for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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