A
A
Andrey Akimov2015-05-17 17:15:06
C++ / C#
Andrey Akimov, 2015-05-17 17:15:06

Recursive traversal of a directory. What libraries and functions to use?

You need to write a function to traverse the directory, along with subdirectories. Most likely a recursive approach should be used. The problem is that there are no such functions in the ANSI C standard library. The search managed to find out that in C POSIX there is a dirent.h header file that describes the necessary functions, but they seem to be only for UNIX-like operating systems. This header file is not present in the VIsual Studio development environment. But it seems like there are functions findfirstfile( ), findnextfile( ), - which have similar functionality.
This is a learning task, not for "production" - so you need to do at least somehow, just to make it work. The program should demonstrate the work in console mode. The task does not say under what OS the application should be. I think that if possible, then it should be universal, and if not, then it doesn't matter.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Coderast, 2015-05-17
@Coderast

It is hardly universally possible, since C does not contain CRT functions for finding files.
If Windows, then FindFirstFileA and FindNextFileA from windows.h
https://msdn.microsoft.com/ru-ru/library/windows/d...
https://msdn.microsoft.com/ru-ru/library/windows /d...

typedef void (*LPSEARCHFUNC)(LPCTSTR lpszFileName);

BOOL SearchFiles(LPCTSTR lpszFileName, LPSEARCHFUNC lpSearchFunc, BOOL bInnerFolders = TRUE)
{
    LPTSTR part;
    char tmp[MAX_PATH]; 
    char name[MAX_PATH];

    HANDLE hSearch = NULL;
    WIN32_FIND_DATA wfd;
    memset(&wfd, 0, sizeof(WIN32_FIND_DATA));

    
    if(bInnerFolders)
    {
        if(GetFullPathName(lpszFileName, MAX_PATH, tmp, &part) == 0) return FALSE;
        lstrcpy(name, part);
        lstrcpy(part, "*.*");

        
        wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
        if (!((hSearch = FindFirstFile(tmp, &wfd)) == INVALID_HANDLE_VALUE))
        do
        {
            if (!strncmp(wfd.cFileName, ".", 1) || !strncmp(wfd.cFileName, "..", 2))            
            continue;
        
            if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                char next[MAX_PATH];
                if(GetFullPathName(lpszFileName, MAX_PATH, next, &part) == 0) return FALSE;
                lstrcpy(part, wfd.cFileName);
                lstrcat(next, "\\");
                lstrcat(next, name);

                SearchFiles(next, lpSearchFunc, TRUE);
            }
        }
        while (FindNextFile(hSearch, &wfd));

        FindClose (hSearch); 
    }

    if ((hSearch = FindFirstFile(lpszFileName, &wfd)) == INVALID_HANDLE_VALUE) 
        return TRUE;
    do
    if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
    {
        char file[MAX_PATH];
        if(GetFullPathName(lpszFileName, MAX_PATH, file, &part) == 0) return FALSE;
        lstrcpy(part, wfd.cFileName);

        lpSearchFunc(file);
    }
    while (FindNextFile(hSearch, &wfd));
    FindClose (hSearch); 

    return TRUE;
}

How it finds a file by mask, calls a function by a pointer with a prototype:
void FileFound(LPCTSTR lpszFileName)

K
kazhuravlev, 2015-05-17
@kazhuravlev

I don't know about C, but some languages ​​have a walk() function that returns a list of directories and subdirectories in the specified directory golang.org/pkg/path/filepath/#Walk

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question