creating a string split function in C++

Possible Duplicate:
Splitting a string in C++

Im trying to create a function that mimics the behavior of the getline() function, with the option to use a delimiter to split the string into tokens. The function accepts 2 strings (the second is being passed by reference) and a char type for the delimiter. It loops through each character of the first string, copying it to the second string and stops looping when it reaches the delimiter. It returns true if the first string have more characters after the delimiter and false otherwise. The position of the last character is being saved in a static variable. for some reason the the program is going into an infinite loop and is not executing anything:

const int LINE_SIZE = 160;
bool strSplit(string sFirst, string & sLast, char cDelim) {
    static int iCount = 0;
    for(int i = iCount; i < LINE_SIZE; i++) {
        if(sFirst[i] != cDelim)
            sLast[i-iCount] = sFirst[i];
        else {
            iCount = i+1;
            return true;
        }
    }
    return false;
}

The function is used in the following way:

while(strSplit(sLine, sToken, '|')) {
    cout << sToken << endl;
}

Why is it going into an infinite loop, and why is it not working? I should add that i'm interested in a solution without using istringstream , if that's possible.


It is not exactly what you asked for, but have you considered std::istringstream and std::getline ?

// UNTESTED
std::istringstream iss(sLine);
while(std::getline(iss, sToken, '|')) {
  std::cout << sToken << "n";
}

EDIT :

Why is it going into an infinite loop, and why is it not working?

We can't know, you didn't provide enough information. Try to create an SSCCE and post that.

I can tell you that the following line is very suspicious:

       sLast[i-iCount] = sFirst[i];

This line will result in undefined behavior (including, perhaps, what you have seen) in any of the following conditions:

  • i >= sFirst.size()
  • i-iCount >= sLast.size()
  • i-iCount < 0
  • It appears to me likely that all of those conditions are true. If the passed-in string is, for example, shorter than 160 lines, or if iCount ever grows to be bigger than the offset of the first delimiter, then you'll get undefined behavior.


    LINE_SIZE is probably larger than the number of characters in the string object, so the code runs off the end of the string's storage, and pretty much anything can happen.

    Instead of rolling your own, string::find does what you need.

    std::string::size_type pos = 0;
    std::string::size_type new_pos = sFirst.find('|', pos);
    

    The call to find finds the first occurrence of '|' that's at or after the position 'pos'. If it succeeds, it returns the index of the '|' that it found. If it fails, it returns std::string::npos . Use it in a loop, and after each match, copy the text from [pos, new_pos) into the target string, and update pos to new_pos + 1 .


    are you sure it's the strSplit() function that doesn't return or is it your caller while loop that's infinite?

    Shouldn't your caller loop be something like:

    while(strSplit(sLine, sToken, '|')) {
        cout << sToken << endl;
        cin >> sLine >> endl;
    }
    

    -- edit --

    if value of sLine is such that it makes strSplit() to return true then the while loop becomes infinite.. so do something to change the value of sLine for each iteration of the loop.. eg put in a cin ..

    链接地址: http://www.djcxy.com/p/19580.html

    上一篇: C ++从具有多个分隔符的文件中读取矩阵

    下一篇: 在C ++中创建一个字符串拆分函数