2 qq 28254507 qq_28254507 于 2016.02.25 16:25 提问

MFC FindFirstFile()的问题

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

求解惑啊。。

4个回答

Hungryof
Hungryof   2016.02.25 16: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找到的文件和文件/文件夹显示的顺序不一定一样的,因为其对找到的结果不进行排序。

caozhy
caozhy   Ds   Rxr 2016.02.25 18: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;
}


给你写了一个排序的

caozhy
caozhy   Ds   Rxr 2016.02.25 18: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_ToStr
John_ToStr   Rxr 2016.02.25 18: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.**_

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!