Thread: splitting a string

  1. #1
    Registered User
    Join Date
    Nov 2010

    splitting a string

    Hey guys. This is my first time doing anything like this so please don't be too harsh on me. So, I am trying to write a shell. The first step is to take the users input command and split it up into arguments.

    So this is what I've got:

    void parse(string something)
    	int cnt = 0;
    	char *argv[11];
    	string val, part;
    	stringstream unit(something);
    	while(getline(unit, val, ' ') && cnt < 10)
    		part = val;
    		argv[cnt]  = (char*)part.c_str();
    	argv[cnt] = NULL;

    So, I want the method to take the string and split it into parts. The delimiter is an empty space between words. Then, I want it to take each part and throw it into the *argv[]. After its got all the parts the very last part put into the array should be followed by a NULL. Seems alright logically to me.

    But, if I were to put in a for loop to read through the array the result I get is not at all what I want.

    For example if I put in "This is my world" and appended this for loop to the end of that method:

    for(int i = 0; argv[i] != NULL; i++)
            cout << argv[i] << endl;
    for(int i = 0; i < cnt; i++)
            cout << argv[i] << endl;

    I should get this as an output:


    But, in most cases I might get the last 3 lines but the first one will be empty. In others the words will get mixed up or there will be doubles even though i did not put a particular word in my input multiple times. I don't understand why this happens. Any help would be appreciated.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    The most suspect line is the c_str() call, so let's look at the internet for answers. From the reference manual:

    const char* c_str ( ) const;

    Get C string equivalent
    Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

    A terminating null character is automatically appended.

    The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.
    So there is your problem.

    part = val; actually seems to be the line that removes the guarantee (since overloaded operators, especially assignment, are actually member functions dressed up in syntax).

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. String splitting algorithm help
    By (TNT) in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2007, 11:28 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM