char SplitPathList( LPSTR* lpPathList )
{
char cPathList[MAX_PATH];
char cDestination[MAX_PATH];
char cSource[MAX_PATH];
int iCurPos = 0;
memcpy( cPathList, *lpPathList, strlen(*lpPathList)+1 );
while( iCurPos < strlen(*lpPathList) )
{
if( cPathList[iCurPos] == ',' )
{
iCurPos++;
for( int i = iCurPos; i<strlen(cSource) - iCurPos; i++ )
{
// The program crashes on the line below, but not the first times it runs
cSource[i - iCurPos] = cPathList[i];
}
break;
}
cDestination[iCurPos] = cPathList[iCurPos];
iCurPos++;
}
DISPLAYMSG( cSource );
DISPLAYMSG( cDestination );
*lpPathList = cSource;
return *cDestination;
}
Trying to take out a piece of a char
I´m trying to take out a piece of a char. I have a pathlist, like this: "C:\Dir\File.ext, C:\Dir\File2.ext, C:\Dir\File3.ext" (lpPathList) and then I want to put the first path in a char (cDestination) and the rest in an other char (cSource). I have´t got the code working, it crashes on a line after it have ran the line some times. Do anyone know why?
Here´s the code:
[edited by - peter86 on October 17, 2002 4:29:13 PM]
kuphryn suspicion is correct. The length of the uninitalized string cSource is used in the following 'for' loop:
Making it's upper bound unknown (and probably causing (i - iCurPos) to exceed MAX_PATH).
[edited by - Solo on October 17, 2002 11:39:51 PM]
for( int i = iCurPos; i<strlen(cSource) - iCurPos; i++ ){ // The program crashes on the line below, but not the first times it runs cSource[i - iCurPos] = cPathList[i];}
Making it's upper bound unknown (and probably causing (i - iCurPos) to exceed MAX_PATH).
[edited by - Solo on October 17, 2002 11:39:51 PM]
I´ve changed the for-loop to this:
for( int i = iCurPos; iBut the output is still full of crap. Why?
for( int i = iCurPos; iBut the output is still full of crap. Why?
Grrr... I had a nice lengthy reply and there was an "interal server error" and I lost it. Somewhat summarized:
- The for loop:
- cDestination doesn't get a null character to terminate the string
- return values:
-- cSource is going to be deallocated when the function exits. lpPathList ends up pointing to unused memory from cSource that will be written over. You need to write your output directly to lpPathList. When you do this lpPathList can be a LPSTR instead of LPSTR*
-- the function only returns one character of cDestination, probably 'C'. Add another LPSTR parameter to the function write data directly to it.
- this code doesn't put a null character at the end of lpDestination if lpPathList has no commas.
Nearly equilavent code with C standard functions and funky pointer math
[edited by - jediknight219 on October 18, 2002 4:30:07 PM]
- The for loop:
for ( int i = iCurPos; i<strlen(cPathList) + 1; i++ )
- cDestination doesn't get a null character to terminate the string
- return values:
-- cSource is going to be deallocated when the function exits. lpPathList ends up pointing to unused memory from cSource that will be written over. You need to write your output directly to lpPathList. When you do this lpPathList can be a LPSTR instead of LPSTR*
-- the function only returns one character of cDestination, probably 'C'. Add another LPSTR parameter to the function write data directly to it.
- this code doesn't put a null character at the end of lpDestination if lpPathList has no commas.
void SplitPathList( LPSTR lpPathList, LPSTR lpDestination ){ char cPathList[MAX_PATH]; int iCurPos = 0; memcpy( cPathList, lpPathList, strlen(lpPathList)+1 ); while( iCurPos < strlen(cPathList) ) { if( cPathList[iCurPos] == ',' ) { lpDestination[iCurPos] = '\0' iCurPos++; for( int i = iCurPos; i<strlen(cPathList) + 1; i++ ) { lpPathList[i - iCurPos] = cPathList[i]; } break; } lpDestination[iCurPos] = cPathList[iCurPos]; iCurPos++; } DISPLAYMSG( lpPathList ); DISPLAYMSG( lpDestination );}
Nearly equilavent code with C standard functions and funky pointer math
void SplitPathList( LPSTR lpPathList, LPSTR lpSource, LPSTR lpDestination ){ LPSTR afterthecomma; afterthecomma = strrchr(lpPathList, ','); strcpy(lpSource, afterthecomma + 1); strncpy(lpDestination, lpPathList, afterthecomma - lpPathList); lpDestination[afterthecomma - lpPathList] = '\0';}
[edited by - jediknight219 on October 18, 2002 4:30:07 PM]
There''s no need for you to code this up by yourself, you have several easier options:
1) strtok() if you must stick with char arrays.
2) Use std::string class, and the find_first_of() and substr() methods to break it apart.
3) boost (www.boost.org) has a tokenizer class that does this also. I haven''t used it, but I''m sure it''s great.
As for why it''s not working, I haven''t tried to figure it out.
1) strtok() if you must stick with char arrays.
2) Use std::string class, and the find_first_of() and substr() methods to break it apart.
3) boost (www.boost.org) has a tokenizer class that does this also. I haven''t used it, but I''m sure it''s great.
As for why it''s not working, I haven''t tried to figure it out.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement