Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

file listing win98 c++

Options
  • 12-12-2003 11:33pm
    #1
    Posts: 0


    Hey all,

    Tis been a while since I posted here...good to be back :)

    Just a small question regarding c++ in windows...

    Is it easy to get a complete file listing of a directory from c++?

    I presume there are no such functions built into c++ for windows, so would I have to use the win32 API?

    Hope this question doesn't seem like a stupid or overly obvious one, I've been searching for something like this for a while, can't seem to come up with anything.

    Any help would be greatly appreciated, thanks.


Comments

  • Closed Accounts Posts: 23 Clod




  • Closed Accounts Posts: 493 ✭✭muffen


    FindFirstFile, FindNextFile and FindClose.

    All you need is to use those API's in a loop (well, the FindNext in a loop) and you're done.

    Also, make sure to check the attributes of the files you get returned, so you know which one is a folder and which one is a file.

    Also make sure to skip '.' and '..'

    Check the MSDN libraries (msdn.microsoft.com) for information about the APIs.
    If you only know how to search the MSDN libraries, you can pretty much write anything you want. They contains tons of good information.
    Considering the installation for the libraries is 2gigs, it better be good I guess :)

    Hope this helps...


  • Registered Users Posts: 885 ✭✭✭clearz


    Try this.

    [PHP]
    #include <windows.h>
    #include <iostream>
    using namespace std;

    int main()
    {
    HANDLE hFind;
    WIN32_FIND_DATA FindData;
    int ErrorCode;
    BOOL Continue = TRUE;

    hFind = FindFirstFile("C:\\Windows\\*", &FindData);

    if(hFind == INVALID_HANDLE_VALUE)
    {
    ErrorCode = GetLastError();
    if (ErrorCode == ERROR_FILE_NOT_FOUND)
    {
    cout << "There are no files matching that path/mask\n" << endl;
    }
    else
    {
    cout << "FindFirstFile() returned error code " << ErrorCode << endl;
    }
    Continue = FALSE;
    }
    else
    {
    cout << FindData.cFileName << endl;
    }

    if (Continue)
    {
    while (FindNextFile(hFind, &FindData))
    {
    cout << FindData.cFileName << endl;
    }

    ErrorCode = GetLastError();

    if (ErrorCode == ERROR_NO_MORE_FILES)
    {
    cout << endl << "All files logged." << endl;
    }
    else
    {
    cout << "FindNextFile() returned error code " << ErrorCode << endl;
    }

    if (!FindClose(hFind))
    {
    ErrorCode = GetLastError();
    cout << "FindClose() returned error code " << ErrorCode << endl;
    }
    }

    return 0;
    }

    [/PHP]


    There should be double backslashes in the dir path e.g C:\\Windows\\
    The star is a wildcard


Advertisement