ProgJ
2016-02-25 08:25
采纳率: 88.1%
浏览 2.1k

MFC FindFirstFile()的问题

程序运行没问题。
但是我有个疑问。。见图:
图片说明
我是用了FindFirstFile()和FindNextFile()遍历所有文件夹,上图是我读取的路径,里面有两个文件夹,一个默认,一个新建,但是我发现读取时FindFirstFile()读取的先是新建的文件夹名称,然后才是默认的文件夹,难道不应该先读取默认文件夹吗?

求解惑啊。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • Hungryof 2016-02-25 08:41
    已采纳

    https://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx 上面的Remark写了原因:
    The FindFirstFile function opens a search handle and returns information about the first file that the file system finds with a name that matches the specified pattern. This may or may not be the first file or directory that appears in a directory-listing application (such as the dir command) when given the same file name string pattern. This is because FindFirstFile does no sorting of the search results.

    FindFirstFile找到的文件和文件/文件夹显示的顺序不一定一样的,因为其对找到的结果不进行排序。

    点赞 打赏 评论
  • blownewbee 2016-02-25 10:06
     // app1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "app1.h"
    #include <vector>  
    #include <algorithm>
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // The one and only application object
    
    CWinApp theApp;
    
    using namespace std;
    
    int cmp(const char * &a, const char * &b)
    {
        return strcmp(a, b) < 0;
    }
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        int nRetCode = 0;
    
        // initialize MFC and print and error on failure
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            cerr << _T("Fatal Error: MFC initialization failed") << endl;
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.
            //CString strHello;
            //strHello.LoadString(IDS_HELLO);
            //cout << (LPCTSTR)strHello << endl;
            _WIN32_FIND_DATAA fd;
            vector<char *> list;
            char * filename;
            HANDLE h = FindFirstFile("c:\\*.*", &fd);
            int r = 1;
            while (h != INVALID_HANDLE_VALUE && r != 0)
            {
                if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
                    {
                        filename = new char[_MAX_PATH + 1];
                        strcpy(filename, fd.cFileName);
                        list.push_back(filename);
                    }
                }
                r = FindNextFile(h, &fd);
            }
            sort(list.begin(), list.end(), cmp);
            for (vector<char *>::iterator it = list.begin(); it != list.end(); it++)  
            {  
                cout<< *it << endl; 
            } 
        }
        return nRetCode;
    }
    
    
    

    给你写了一个排序的

    点赞 打赏 评论
  • blownewbee 2016-02-25 10:08

    $Recycle.Bin
    Documents and Settings
    PerfLogs
    Program Files
    ProgramData
    System Volume Information
    Users
    Windows
    Press any key to continue

    如果return strcmp(a, b) < 0;修改为
    return strcmp(a, b) > 0;

    Windows
    Users
    System Volume Information
    ProgramData
    Program Files
    PerfLogs
    Documents and Settings
    $Recycle.Bin
    Press any key to continue

    另外请注意,这段演示的代码没有释放vector里每个char *,但是实际使用,需要释放下。

    点赞 打赏 评论
  • John_ToDebug 2016-02-25 10:29

    http://www.cnblogs.com/chenkunyun/archive/2012/03/24/2415727.html
    并没有绝对的顺序:_**This may or may not be the first file or directory that appears in a directory-listing application (such as the dir command) when given the same file name string pattern. This is because FindFirstFile does no sorting of the search results.**_

    点赞 打赏 评论

相关推荐 更多相似问题