Re: My own fuction: Access Violation error...
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Mon, 10 Mar 2008 15:43:22 -0500
Note that strtok is not a particularly well-designed library function, but its use here is
OK.
And yes, you are doing something blatantly wrong. Read the description of _tcstok VERY
carefully. On subsequent calls, you need to pass NULL in as the first argument.
There are many potential problems here. I assume there is a deep and compelling reason
for using the obsolete 'char' type to represent text? Modern code is always
Unicode-aware, and would be written as
vector<LPTSTR> Split(LPTSTR input, LPCTSTR sep)
{
vector<LPTSTR>v;
LPTSTR token = _tcstok(input, sep);
while(token != NULL)
{
v.push_back(token);
token = _tcstok(NULL, sep);
}
return v;
}
Until this bug is fixed, looking for other problems does not matter. If you still have
problems after fixing the code, post again.
joe
On Mon, 10 Mar 2008 13:13:19 -0700 (PDT), Blue Streak <rdlebreton@xxxxxxxxxxx> wrote:
Hello,Joseph M. Newcomer [MVP]
I am trying to implement my own version a split function in VC6 using
a vector and strtok().
vector<char *> Split(char *input, const char *sep)
{ //implementation of VB split function using vectors
vector<char *> v;
char *token;
token = " ";
while (token != NULL)
{
token = strtok(input, sep);
v.push_back(token);
}
return v;
}
The only problem is when I trace it through the first token and I have
a space as the separator (haven't tried another separator, yet) it
crashes and burns (i.e. Access Violation) when it reaches the first
separator / space.
Am I doing something blatantly wrong?
Any suggestions to improve this function?
TIA...
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: My own fuction: Access Violation error...
- From: Blue Streak
- Re: My own fuction: Access Violation error...
- From: Giovanni Dicanio
- Re: My own fuction: Access Violation error...
- References:
- My own fuction: Access Violation error...
- From: Blue Streak
- My own fuction: Access Violation error...
- Prev by Date: Re: My own fuction: Access Violation error...
- Next by Date: Re: HTREEITEM
- Previous by thread: Re: My own fuction: Access Violation error...
- Next by thread: Re: My own fuction: Access Violation error...
- Index(es):
Relevant Pages
|