Skip to main content
gamedev.net
🔒 Locked

FindFirstFile Problems

Started by Programmer101 Jan 25, 2008 at 7:41 AM 3 replies 750+ views
Original Post
Programmer101
Programmer101
Hello. Recently I have been trying to make a program that will list all files and sub directories in a given directory. I found some code on an older thread in here and tried to use it. I had to change it around a bit for it to run but it still has issues. The first problem is that the program only seems to find directories and not actual files. The second problem is that the directory names that it does retrieve are nothing but random characters that don't make any sense (like TaSLvpCiiLRsTSxld~~). Here is my code:
void searchDirectory(string start)
{
	bool             fFinished;
    HANDLE           hList;
    char             szDir[MAX_PATH+1];
    char             szSubDir[MAX_PATH+1];
	char             szFileName[MAX_PATH+1];
    WIN32_FIND_DATAW FileData;

    // Get the proper directory path (doesn't do anything now)
    sprintf(szDir, "%s\\*", start);

    // Get the first file
	hList = FindFirstFileW(L"E:\\*.*", &FileData);
    if (hList == INVALID_HANDLE_VALUE)
    { 
        printf("No files found\n\n");
    }
    else
    {
        fFinished = false;
        while (!fFinished)
        {
            // Check the object is a directory or not
            if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                //Make sure directory is not ""
				if ((strcmp((const char *)FileData.cFileName, ".") != 0) &&
(strcmp((const char *)FileData.cFileName, "..") != 0))
                {
                    sprintf(szFileName, "\nFound Directory: %s", FileData.cFileName);
					printf("%s", szFileName);
                }
            }
            else
                printf("%s", FileData.cFileName);


			//Check to see if finished
            if (!FindNextFileW(hList, &FileData))
            {
                if (GetLastError() == ERROR_NO_MORE_FILES)
                {
                    fFinished = true;
                }
            }
        }
    }

    FindClose(hList);
}

Thanks
SiCrane
SiCrane
You are using wide character versions of functions and treating the results as narrow characters. This is bad, bad, mojo. Just use the narrow character versions of the functions. Also, consider using a more typesafe library like boost::filesystem instead.
Evil Steve
Evil Steve
Alternatively, if you can't use boost and you have to use wide characters, use %S (capital S) as the format param to print wide character strings, e.g.
printf("%S", FileData.cFileName);
SiCrane
SiCrane
%S is a non-standard extension, and it won't help with the other places he's mixing wide chars and narrow chars.
Evil Steve
Evil Steve
Quote:
Original post by SiCrane
%S is a non-standard extension, and it won't help with the other places he's mixing wide chars and narrow chars.
Oh, I didn't realise it was non-standard.

I should also add that you should really avoid any *printf() calls unless you have no other choice (E.g. you're using C rather than C++).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.