Thursday, 22 August 2013

C++ strtok - multiple use with more data buffers

C++ strtok - multiple use with more data buffers

I have little issue with using strtok() function. I am parsing two files.
Firts I load file 1 into buffer. This file constains name of the second
file I need to load. Both files are read line after line. My code looks
like this:
char second_file_name[128] = { "" };
char * line = strtok( buffer, "\n" );
while( line != NULL )
{
if ( line[0] = 'f' )
{
sscanf( line, "%*s %s", &second_file_name );
LoadSecondFile( second_file_name );
}
// processing other lines, not relevant for question
line = strtok( NULL, "\n" );
}
While the LoadSecondFile(...) function works in pretty same way, thus:
char * line = strtok( buffer, "\n" );
while( line != NULL )
{
// process file data
line = strtok( NULL, "\n" );
}
What my problem is, after calling the LoadSecondFile(...) function, the
strtok() pointer used for parsing the first file gets "messed up". Instead
of giving me line that follows the name of the second file, it gives me
nothing - understand as "complete nonsense". Do I get it right that this
is caused by strtok() pointer being shared in program, not only in
function? If so, how can I "back up" the pointer of strtok() used for
parsing first file before using it for parsing second file?
Thanks for any advice. Cheers.

No comments:

Post a Comment