码迷,mamicode.com
首页 > 其他好文 > 详细

访问指定路径下的目录以及文件

时间:2014-07-18 18:36:12      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

#include "stdafx.h"
//vs2010下运行通过
#undef UNICODE
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <iostream>
using namespace std;

void browseFile(char* path)
{
    char pattern[FILENAME_MAX + 1];
    sprintf(pattern, "%s\\*.*", path);

    char fdPath[FILENAME_MAX + 1];//file or document path

    WIN32_FIND_DATA findFileData;
    HANDLE hFindFile = FindFirstFile(pattern, &findFileData);

    if (hFindFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (strcmp(findFileData.cFileName, ".") == 0 || strcmp(findFileData.cFileName, "..") == 0)
            {
                continue;
            }

            sprintf(fdPath, "%s\\%s", path, findFileData.cFileName);
            if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                cout<<fdPath<<endl;//对目录做一些操作
                browseFile(fdPath);//访问目录下面的内容
                
            }
            else
            {
                cout<<"\t"<<findFileData.cFileName<<endl;//对文件做一些操作
            }
        }
        while (FindNextFile(hFindFile, &findFileData));
    }
    FindClose(hFindFile);
}
int _tmain(int argc, _TCHAR* argv[])
{

    browseFile("C:\\Users\\ydu1\\Desktop\\ffff");

    return 0;
}
 

bubuko.com,布布扣

注意:

要去掉UNICODE宏定义,否则会出现FindFirstFileW”: 不能将参数 1 从“char [261]”转换为“LPCWSTR,参考http://bbs.csdn.net/topics/120047056

访问指定路径下的目录以及文件,布布扣,bubuko.com

访问指定路径下的目录以及文件

标签:des   style   blog   http   color   os   

原文地址:http://www.cnblogs.com/dy-techblog/p/3853361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!