In short, the problem is that it is impossible to get into other disks and folders (higher than the executable file is).
In general, the strong feeling that the function is written is very crooked and not correct. Tell me how to rewrite it correctly

bool Search_File(TCHAR *buffer, char mas[]) { HANDLE file; WIN32_FIND_DATA ffd = {0}; TCHAR temp[MAX_PATH]; TCHAR str[MAX_PATH]; SetCurrentDirectory(buffer);//текуций католог file = FindFirstFile(TEXT(mas),&ffd); if (file == INVALID_HANDLE_VALUE) { cout <<"Файл не был найден"<<endl; return false; } else{ do{ if(!strcmp(ffd.cFileName, TEXT("."))||!strcmp(ffd.cFileName, TEXT(".."))){ continue; } if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){//если не установлен бит, тогда файл strcpy(temp, buffer); strcat(temp, "\\"); strcat(temp, ffd.cFileName); Search_File(buffer, mas); strcpy(buffer, temp); continue; } cout<<"найденый файл:"<<ffd.cFileName<<endl; } while(FindNextFile(file,&ffd) != 0); } FindClose(file); return true; } 
  • you can use filesystem :: recursive_directory_iterator, for example - jfs

1 answer 1

Pay attention to the following points:

First, the SetCurrentDirectory () function is used incorrectly, which is fraught with further errors as the program code grows. The fact is that the current directory is a global variable of the process and to change it in this way, in one of the working functions, and even more so - not to restore it - is an incorrect practice. If we deal with a multi-threaded application and file streams are used in different streams (and as a rule, this happens in large programs — protocols, logs, bug reports, options are read / written, etc.), then a random change of the current directory is unacceptable. If this function is used, then usually at the time of initialization of the program at startup and most (if not all) file operations are performed along relative paths (relative to the current one).

Therefore, I recommend removing the SetCurrentDirectory () call from the function and passing the Search_File () function to the full path + mask of the required file (remember that you can use the characters * and? In FindFirstFile () ) or form this full path dynamically by joining the mas [[ ] to buffer.

Secondly, in certain cases (when the target name contains wildcard characters, a dot or a directory name), you must have access rights to the root directory and to all subdirectories in the search path for a successful search.

  • how is it dynamic? - SàshàRozinov
  • In the sense that the full search path is formed directly in the Search_File () function of mas [] and buffer at its inputs: 1) either add a memory buffer that the function uses to concatenate strings, increasing / decreasing its size if necessary; 2) either we transfer the buffer with a margin of free space at the end, so that it can be attached to mas [], and after the search is completed, trim again by writing a null character - Denys Save