Solved

Sorting a class vector and strtok???

Posted on 2004-08-22
4
273 Views
Last Modified: 2010-04-01
Hi.

class CClass{
    private:
        string myStr;
    public:
        void setMyStr(string a);
        string getMyString();
}


void someMethod(){
    sort(myCClass.begin(), myCClass.end(), someSortMethod);
}


void someSortMethod(CClass a, CClass b){
    string fst=a.getMyString();
    string snd=b.getMyString();

    char *part1;
    char *part2;
    char *part3;

    part1=strtok((char*)fst.c_str(), "*");
    part2=strtok(NULL, "*");
    part3=strtok(NULL, "*");
}


myStr is in the format:

    NUMBER*NUMBER*NUMBER

However, when this line is done:

    part1=strtok((char*)fst.c_str(), "*");

then it modifies the fst.myStr variable, making the further strtok calls crash. How can I keep this from happening?
0
Comment
Question by:Unimatrix_001
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 280 total points
Comment Utility
First of all, part1,2,3 are assigned but not used.

when you assign one string to the other, a copy is not created, just a reference to the first, like a Windows File Shorcut.
So, strtok will affect fst too. Strtok is not well suited to handle a STL string buffer.
To avoid this, you can create a copy in a standard C buffer, and apply strtok to it, something like:

char fst[234];  // any buffer size
strcpy(fst, a.c_str(), a.size());

Then you can apply strtok without worrying:
part1=strtok(fst, "*");
part2=strtok(NULL, "*");
part3=strtok(NULL, "*");

0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 130 total points
Comment Utility
I recommend using C++ approach since you're using C++ STL objects.

Example code:

int main(int argc, char* argv[])
{
      std::string Data = "NUMBER*NUMBER*NUMBER*"; //Added extra asteric for simplicity
      
      int part1 = Data.find('*');
      int part2 = Data.find('*', part1+1);
      int part3 = Data.find('*', part2+1);

      cout << Data.substr(0, part1) << endl;
      cout << Data.substr(part1+1, (part2-part1)-1) << endl;
      cout << Data.substr(part2+1, (part3-part2)-1) << endl;

FYI:
>>when you assign one string to the other, a copy is not created, just a reference to the first
This is called reference counting, and almost all implementations use this approach, but according to the C++ standard, they don't have to.

You should never cast an std::string buffer to a non-constant C style string.
There are too many things that can go wrong.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 90 total points
Comment Utility
>>    part1=strtok((char*)fst.c_str(), "*");

You have noticed that 'c_str()' returns a 'const char*'?

As jaime_olivares already suggested, store your data in a different place for tokenizing it. But to overcome the inherent danger of a buffer overflow with a fixed size array, I'd suggest to either use

char* tmp = strdup ( fst.c_str());

part1=strtok(tmp, "*");

//...

free ( tmp);

or

char* tmp = new char [ fst.c_str()];
strcpy(fst, a.c_str(), a.size());

part1=strtok(tmp, "*");

//...

delete [] tmp;


0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
Points split :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
TTimer no longer functioning 7 74
returning a dereferenced pts in C++ 10 130
How to convert MFC APP to Win32 APP. 19 53
Dialogbox API leak? 18 57
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now