C++ strtok - multiple use with more data buffers

馋奶兔 提交于 2020-01-11 11:42:31

问题


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.


回答1:


strtok is an evil little function which maintains global state, so (as you've found) you can't tokenise two strings at the same time. On some platforms, there are less evil variants with names like strtok_r or strtok_s; but since you're writing C++ not C, why not use the C++ library?

ifstream first_file(first_file_name);      // no need to read into a buffer
string line;
while (getline(first_file, line)) {
    if (!line.empty() && line[0] == 'f') { // NOT =
        istringstream line_stream(line);
        string second_file_name;
        line_stream.ignore(' ');           // skip first word ("%*s")
        line_stream >> second_file_name;   // read second word ("%s")
        LoadSecondFile(second_file_name);
    }
}



回答2:


You can use strtok_r which allows you to have different state pointers.




回答3:


Which is why it is constantly recommended to not use strtok (not to mention the problems with threads). There are many better solutions, using the functions in the C++ standard library. None of which modify the text they're working on, and none of which use hidden, static state.



来源:https://stackoverflow.com/questions/18379305/c-strtok-multiple-use-with-more-data-buffers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!