2017-05-10 2 views
-2

J'ai rencontré ce code pour la liste de répertoires sur Windows en utilisant C++, et j'ai compris que vous pouvez rechercher un répertoire avec ces API: FindFirstFileEx, FindNextFile et CloseFind. Vous aurez besoin de #include, cela vous donnera accès à l'API Windows.Erreur dans la liste du répertoire itératif en utilisant C++

Je ne suis pas capable de comprendre comment ce code fonctionne, par conséquent, pas en mesure de faire l'erreur de compilation.

#include <windows.h> 
#include <tchar.h> 
#include <stdio.h> 
#include <strcat.h> 
#pragma comment(lib, "User32.lib") 

void DisplayErrorBox(LPTSTR lpszFunction); 

int _tmain(int argc, TCHAR *argv[]) 
{ 
WIN32_FIND_DATA ffd; 
LARGE_INTEGER filesize; 
TCHAR szDir[MAX_PATH]; 
size_t length_of_arg; 
HANDLE hFind = INVALID_HANDLE_VALUE; 
DWORD dwError=0; 

// If the directory is not specified as a command-line argument, 
// print usage. 

if(argc != 2) 
{ 
    _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]); 
    return (-1); 
} 

// Check that the input path plus 3 is not longer than MAX_PATH. 
// Three characters are for the "\*" plus NULL appended below. 

StringCchLength(argv[1], MAX_PATH, &length_of_arg); 

if (length_of_arg > (MAX_PATH - 3)) 
{ 
    _tprintf(TEXT("\nDirectory path is too long.\n")); 
    return (-1); 
} 

_tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]); 

// Prepare string for use with FindFile functions. First, copy the 
// string to a buffer, then append '\*' to the directory name. 

StringCchCopy(szDir, MAX_PATH, argv[1]); 
StringCchCat(szDir, MAX_PATH, TEXT("\\*")); 

// Find the first file in the directory. 

hFind = FindFirstFile(szDir, &ffd); 

if (INVALID_HANDLE_VALUE == hFind) 
{ 
    DisplayErrorBox(TEXT("FindFirstFile")); 
    return dwError; 
} 

// List all the files in the directory with some info about them. 

do 
{ 
    if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
    { 
     _tprintf(TEXT(" %s <DIR>\n"), ffd.cFileName); 
    } 
    else 
    { 
     filesize.LowPart = ffd.nFileSizeLow; 
     filesize.HighPart = ffd.nFileSizeHigh; 
     _tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName, filesize.QuadPart); 
    } 
} 
while (FindNextFile(hFind, &ffd) != 0); 

dwError = GetLastError(); 
if (dwError != ERROR_NO_MORE_FILES) 
{ 
    DisplayErrorBox(TEXT("FindFirstFile")); 
} 

FindClose(hFind); 
return dwError; 

}

void DisplayErrorBox(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code 

LPVOID lpMsgBuf; 
LPVOID lpDisplayBuf; 
DWORD dw = GetLastError(); 

FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM | 
    FORMAT_MESSAGE_IGNORE_INSERTS, 
    NULL, 
    dw, 
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    (LPTSTR) &lpMsgBuf, 
    0, NULL); 

// Display the error message and clean up 

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
     (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
StringCchPrintf((LPTSTR)lpDisplayBuf, 
    LocalSize(lpDisplayBuf)/sizeof(TCHAR), 
    TEXT("%s failed with error %d: %s"), 
    lpszFunction, dw, lpMsgBuf); 
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

LocalFree(lpMsgBuf); 
LocalFree(lpDisplayBuf); 
} 

Donne l'erreur de followoing sur codeblocks:

fatal error: strcat.h: No such file or directory 
    #include <strcat.h> 
       ^

terminé la compilation.

+0

'' #include -> '' #include

+0

Theres tout beaucoup d'erreur que je reçois ici: (erreur: 'StringCchLength' n'a pas été déclaré dans ce cadre StringCchLength (argv [1], MAX_PATH, & length_of_arg);) ^ Encore une fois, (erreur: 'StringCchCopy' n'a pas été déclaré dans ce champ d'application StringCchCopy (szDir, MAX_PATH, argv [1]);) en outre, (erreur: 'StringCchCat' était pas déclaré dans ce champ StringCchCat (szDir, MAX_PAT H, TEXTE ("\\ *")); – rhym1n

+0

Google "StringCchCat", premier coup et lu. –

Répondre

-1

strcat est défini dans string.h pas strcat.h

Ensuite, vous ne semblez pas être en utilisant strcat, vous pouvez peut-être enlever cela.

+0

L'erreur que je reçois sur l'écriture est: erreur: « StringCchLength » n'a pas été déclaré dans ce champ – rhym1n

+0

Cette fonction est définie dans '' . Cet en-tête fait partie du SDK Windows. – arboreal84

+0

Lors de l'utilisation de cet en-tête Erreur irrécupérable: strsafe.h: Aucun fichier ou répertoire de ce type #include rhym1n