0

I need help changing my return type from int to vector<string>.. I don't want to have to pass the vector as a pointer to the function but rather I just want to return the vector itself as I no longer need to check the return type. If it fails, it'd return an empty vector.

I was thinking to declare a vector inside the function and the loop the function's internals but then I got stuck at the If Success check because it starts the function all over again.

I changed it from StringArray type to a vector of strings so it's compilable for anyone helping.

#define Repeat do{
#define Until(condition) }while(!(condition));


#include <string>
#include <vector>
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

int SearchDirectory(vector<string> &FilesFound, string RootDirectory, string FileExtension, bool SearchSubdirectories = false)
{
    string  FilePath;                   // Filepath
    string  Pattern;                    // Pattern
    string  Extension;                  // Extension
    HANDLE  hFile;                      // Handle to file
    WIN32_FIND_DATA FileInformation;    // File information


    Pattern = RootDirectory + "/*.*";

    hFile = FindFirstFile(Pattern.c_str(), &FileInformation);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        Repeat //A simple do-while loop with an opposite condition.. Ported from pascal.
            if(FileInformation.cFileName[0] != '.')
            {
                FilePath.erase();
                FilePath = RootDirectory + "/" + FileInformation.cFileName;

                if(FileInformation.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
                {
                    if(SearchSubdirectories)
                    {
                        // Search subdirectory
                        int Success = SearchDirectory(FilesFound, FilePath, FileExtension, SearchSubdirectories);     //Recursive call.. Not sure if I'll overflow the stack here.. Any better ways to do this?
                        if(Success)
                            return Success;
                    }
                }
                else
                {
                    //Check extension
                    Extension = FileInformation.cFileName;
                    Extension = Extension.substr(Extension.rfind(".") + 1); //Find the last dot as that is the extension..

                    if(Extension == FileExtension)
                    {
                        FilesFound.push_back(FilePath);  //Add file to StringArray.
                    }
                }
            }
        Until(FindNextFile(hFile, &FileInformation) != TRUE);

        FindClose(hFile);   //CloseFile Handle

        DWORD dwError = GetLastError();
        if(dwError != ERROR_NO_MORE_FILES)
            return dwError;
    }
    return 0;
}

int main()
{
    vector<string> X;    //StringArray X; Changed to vector so others can compile it.
    SearchDirectory(X, "C:/Testing", "dat", true);
    for (unsigned I = 0; I < X.size(); I++)
        cout<<X[I]<<"\n";
}

Edited by triumphost: n/a

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by mazzica1
1

you have 2 options:
1- use pointer and return Null and check on it
2-return vector<string> and check on size and for recursive part:

// Search subdirectory
vector<string> tmp = SearchDirectory(FilePath, FileExtension, SearchSubdirectories);     //Recursive call.. Not sure if I'll overflow the stack here.. Any better ways to do this?
for(int i=0;i<tmp.size();i++)
{
	FilesFound.push_back(tmp.at(i));
}
tmp.clear();
Votes + Comments
Epic! Flawless post :D
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.