find last of



  • Hallo, ich bin zimlich neue in C++ und möchte gerne ein Programm schreiben, das einen Ordner ausliest und HPP und CPP Dateien trennt.

    Hier ist meine Funktion:

    bool findFiles(std::vector<std::string>& cppFiles,
    			   std::vector<std::string>& hppFiles,
    			   const boost::filesystem::path& path)
    {
    	try 
    	{	
    		boost::filesystem::directory_iterator end_of_dir;
    		for( boost::filesystem::directory_iterator iter(path);
    			 iter != end_of_dir;
    			 ++iter )
    		{
    			if(boost::filesystem::is_regular_file(*iter))
    			{
    				std::string currentFile = iter->path().string();
    
    				if( currentFile.find_last_of(".cpp") != std::string::npos ||
    					currentFile.find_last_of(".cc")  != std::string::npos )
    				{
    					cppFiles.push_back(currentFile);
    				}
    
    				if( currentFile.find_last_of(".h")   != std::string::npos ||
    					currentFile.find_last_of(".hpp") != std::string::npos )
    				{
    					hppFiles.push_back(currentFile);
    				}
    			}
    		}
    		return true;
    	}
    	catch(boost::filesystem::filesystem_error& error)
    	{
    		std::cout << "Error: " << error.what() << std::endl;
    		return false;
    	}
    }
    

    Leider kommen in beiden vectoren alle Dateien was mache ich falsch?



  • was machst find_last_of denn?

    C++.com

    Array with a sequence of characters. The last character of the string that compares equal to any of the characters in this sequence is considered a match.

    d.h. sobald auch nur ein zeichen aus deinem paramter string übergeben wird, denk er schon "ja genau das will mein chef da haben..."
    aber das willst du ja eben nicht, sondern eben der gesamte string soll vorkommen

    für dich ist find was besser geeignet, bzw rfind(da sucht er von hinten)
    guckst du hier

    edit:
    so in etwa (ist aber nicht so schön irgendwie, finde ich + ungetestet)

    bool findFiles(std::vector<std::string>& cppFiles,
                   std::vector<std::string>& hppFiles,
                   const boost::filesystem::path& path)
    {
        try
        {   
            boost::filesystem::directory_iterator end_of_dir;
            for( boost::filesystem::directory_iterator iter(path);
                 iter != end_of_dir;
                 ++iter )
            {
                if(boost::filesystem::is_regular_file(*iter))
                {
                    std::string currentFile = iter->path().string();
    
    				size_t found1 = currentFile.rfind(".cpp");
    				size_t found2 = currentFile.rfind(".cc");
                    if( found != std::string::npos ||
                        found2 != std::string::npos )
                    {
                        cppFiles.push_back(currentFile);
                    }
    
    				found1 = currentFile.rfind(".h");
    				found2 = currentFile.rfind(".hpp");
                    if( found1   != std::string::npos ||
                        found2 != std::string::npos )
                    {
                        hppFiles.push_back(currentFile);
                    }
                }
            }
            return true;
        }
        catch(boost::filesystem::filesystem_error& error)
        {
            std::cout << "Error: " << error.what() << std::endl;
            return false;
        }
    }
    


  • Wenn du schon boost einsetzt, dann kannst du iends_with benutzen.



  • Danke, iends_with ist genau das was ich wollte 🙂


Anmelden zum Antworten