如下:
   程序自动实现获取某盘符下所有文件名?
例如:我的c盘下有目录:winnt program document 程序执行後自动记录这些目和子目录的名字。
保存在notepad中。

解决方案 »

  1.   

    #define ErrorHandler(a)
    WIN32_FIND_DATA FileData; 
    HANDLE hSearch; 
    BOOL fFinished = FALSE; 
     
    // Start searching for .TXT files in the current directory. 
     
    hSearch = FindFirstFile("c:\\*.*", &FileData); 
    if (hSearch == INVALID_HANDLE_VALUE) 

        ErrorHandler("No files found."); 

     
    // Copy each .TXT file to the new directory 
    // and change it to read only, if not already. 
     
    while (!fFinished) 

        FileData.cFileName;//就是
        if (!FindNextFile(hSearch, &FileData)) 
        {
            if (GetLastError() == ERROR_NO_MORE_FILES) 
            { 
       //         MessageBox( "No more files."); 
                fFinished = TRUE; 
            } 
            else 
            { 
                ErrorHandler("Couldn't find next file."); 
            } 
        }

     
    // Close the search handle. 
     
    if (!FindClose(hSearch)) 

        ErrorHandler("Couldn't close search handle."); 
    }
      

  2.   

    hSearch = FindFirstFile("c:\\*.*", &FileData); 
    请问:这句得作用是什么啊?
      

  3.   

    这里给你一个比较完整的代码,可以实现你的要求:
    //在你需要的地方调用ListFile("你的路径");即可,结果将输出到c:\\result.txt.
    void CFindString::ListFile(CString str)
    {
    CFile f;
    str += "\r\n";
    f.Open("c:\\result.txt",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
    f.SeekToEnd();
    f.Write(str,str.GetLength());
    f.Close();
    }
    void CFindString::ListFolder(CString sPath)
    {
    CFileFind ff;
    BOOL bFound = ff.FindFile(sPath + "\\*.*");
    while(bFound)
    {
    bFound = ff.FindNextFile();
    CString sFilePath = ff.GetFilePath();
    if(ff.IsDirectory())
    {
    if(!ff.IsDots())
    ListFolder(sFilePath);
    }
    else
    ListFile(sFilePath);
    }
    }
      

  4.   

    请问gamezealot(「Ryoga VS 我愛羅」:潜心练剑.挥剑问情...) :这里的Void CFindString::ListFolder(CString sPath)函数的作用是什么?它和前面的Void CFindString::ListFile(CString str)函数之间是什么样的逻辑关系?另外,有些问题如下:
    void CFindString::ListFile(CString str)
    {
    CFile f;
    str += "\r\n";
    f.Open("c:\\result.txt",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
    f.SeekToEnd();//这里是什么意思?
    f.Write(str,str.GetLength());//这里什么意思?
    f.Close();
    }void CFindString::ListFolder(CString sPath)//这里写路径和上面的ListFile(CString str)写的
    //路径有什么关系?
    {
    CFileFind ff;
    BOOL bFound = ff.FindFile(sPath + "\\*.*");
    while(bFound)
    {
    bFound = ff.FindNextFile();
    CString sFilePath = ff.GetFilePath();
    if(ff.IsDirectory())//这句是什么意思?
    {
    if(!ff.IsDots())//这句是什么意思?
    ListFolder(sFilePath);
    }
    else
    ListFile(sFilePath);
    }
    }
      

  5.   

    void ListFile(CString str)
    {
    CFile f;
    str += "\r\n";
    f.Open("c:\\result.txt",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
    f.SeekToEnd();
    f.Write(str,str.GetLength());
    f.Close();
    }
    void ListFolder(CString sPath)
    {
    CFileFind ff;
    BOOL bFound = ff.FindFile(sPath + "\\*.*");
    while(bFound)
    {
    bFound = ff.FindNextFile();
    CString sFilePath = ff.GetFilePath();
    if(ff.IsDirectory())//如果是目录
    {
    if(!ff.IsDots())//每个目录下都有一个名字为..的目录,代表上层目录,所以在枚举的时候要跳过这个特殊目录,否则死循环
    ListFolder(sFilePath);
    }
    else
    ListFile(sFilePath);
    }
    }
      

  6.   

    f.SeekToEnd();//这里是什么意思?:::移动文件指针到文件结尾
    f.Write(str,str.GetLength());//这里什么意思? ::::写入换行(\r\n)+文件名
      

  7.   

    void CFindString::ListFolder(CString sPath)//这里写路径和上面的ListFile(CString str)写的
    //路径有什么关系?