Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何得到最后一个strtok?_C++_String_Split_Strtok - Fatal编程技术网

C++ 如何得到最后一个strtok?

C++ 如何得到最后一个strtok?,c++,string,split,strtok,C++,String,Split,Strtok,我想拆分一个“字符串”(分隔符\)以获取最后一个匹配项。是我写的代码: char str[] ="D:\\Google Drive\\My Files\\Test.zip"; char * buffer = str; sprintf(buffer, "%s", strtok(str,"\\")); cout << buffer; char str[]=“D:\\Google Drive\\My Files\\Test.zip”; char*buffer=str; sprintf(

我想拆分一个“字符串”(分隔符
\
)以获取最后一个匹配项。是我写的代码:

char str[] ="D:\\Google Drive\\My Files\\Test.zip";
char * buffer = str;

sprintf(buffer, "%s", strtok(str,"\\"));
cout << buffer;
char str[]=“D:\\Google Drive\\My Files\\Test.zip”;
char*buffer=str;
sprintf(缓冲区,“%s”,strtok(str,“\\”);
cout改为使用-在这种情况下,它更合适,同时也是非破坏性的和可重入的:

char str[] ="D:\\Google Drive\\My Files\\Test.zip";
char * buffer = strrchr(str, '\\');
cout << buffer + 1;

改用-在这种情况下,它更合适,同时也是非破坏性的和可重入的:

char str[] ="D:\\Google Drive\\My Files\\Test.zip";
char * buffer = strrchr(str, '\\');
cout << buffer + 1;

对不起。但我需要使用/保持sprintf(即格式化字符串方法)。这看起来很愚蠢-但如果您需要的话,我会降低它的效率。这是因为我实际上使用的是WDL_字符串容器。我可以使用“格式字符串”设置/追加字符串。该容器没有.Split函数。别忘了测试
缓冲区!=NULL
,尤其是当要搜索的字符串是用户输入时。嗯,我想这不是使用WDL_字符串进行搜索的最佳方法。你有什么建议?对不起。但我需要使用/保持sprintf(即格式化字符串方法)。这看起来很愚蠢-但如果您需要的话,我会降低它的效率。这是因为我实际上使用的是WDL_字符串容器。我可以使用“格式字符串”设置/追加字符串。该容器没有.Split函数。别忘了测试
缓冲区!=NULL
,尤其是当要搜索的字符串是用户输入时。嗯,我想这不是使用WDL_字符串进行搜索的最佳方法。你有什么建议?为什么你甚至使用C字符串和C API来判断什么是C++程序?为什么不使用C++来编写正确的C++代码:Stry?为什么你甚至使用C字符串和C API来定义什么是C++程序?为什么不使用C++代码来编写正确的代码?例如:代码> STD::String < /C>?