开发者社区> 问答> 正文

MFC FindFirstFile()的问题

程序运行没问题。
但是我有个疑问。。见图:
screenshot

展开
收起
a123456678 2016-03-09 11:15:16 1950 0
1 条回答
写回答
取消 提交回答
  •  // 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;
    }
    
    2019-07-17 18:55:38
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载