smallB
smallB

Reputation: 17128

I have problems with using find_last_of

When I try to find last occurrence of "EndProject" in file, which looks like this: (this is original VS solution file, without any changes):

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ASO", "ASO\ASO.vcxproj", "{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "asdfasdf", "asdfasdf\asdfasdf.vcxproj", "{05527F0D-4B98-4A55-B038-3C60005566CB}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Win32 = Debug|Win32
        Release|Win32 = Release|Win32
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Debug|Win32.ActiveCfg = Debug|Win32
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Debug|Win32.Build.0 = Debug|Win32
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Release|Win32.ActiveCfg = Release|Win32
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Release|Win32.Build.0 = Release|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Debug|Win32.ActiveCfg = Debug|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Debug|Win32.Build.0 = Debug|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Release|Win32.ActiveCfg = Release|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Release|Win32.Build.0 = Release|Win32
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
EndGlobal

and I'm using:

auto end_of_project_manifest = project_file_contents.find_last_of("EndProject");//here project_file_contents is a string filed with contents of this file.

the result I'm getting is 1308, which is strange because this file has only 1313 chars in total. Surely there is something wrong but what?

Upvotes: 1

Views: 530

Answers (1)

hmjd
hmjd

Reputation: 122001

std::string::find_last_of() will find the last occurrence of any of the characters in the search string, not the last occurrence of the entire search string: in this case if finds the o in EndGlobal.

Use std::string::rfind() instead:

auto end_of_project_manifest = project_file_contents.rfind("EndProject");

Upvotes: 11

Related Questions