C语言遍历目录

#include <Windows.h>
#include <stdio.h>
#include <string.h>

void FindDir(char* parent)
{
    HANDLE hFile = NULL;
    WIN32_FIND_DATA fd = {0};
    char tmp[MAX_PATH] = {0};
    char sub[MAX_PATH] = {0};
    sprintf(tmp, "%s\\*.*", parent);
    hFile = FindFirstFile(tmp, &fd);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if(*fd.cFileName == '.')
                    continue;
                printf("%s\\%s\n", parent, fd.cFileName);
                sprintf(sub, "%s\\%s", parent, fd.cFileName);
                FindDir(sub);
            }
        }while(FindNextFile(hFile, &fd));
        FindClose(hFile); hFile = NULL;
    }
}

int main(void)
{
    char parent[MAX_PATH];
    printf("父目录(不包含\"\\\"):");
    fgets(parent, MAX_PATH, stdin);
    *strrchr(parent, '\n') = 0;
    FindDir(parent);
    return 0;
}

女孩不哭(QQ:191035066)@2012-05-26 10:00:07
http://www.cnblogs.com/nbsofer/archive/2012/05/26/2519009.html
http://topic.csdn.net/u/20120512/21/9ae050f8-e5fb-4cc8-856d-42df81078e07.html

你可能感兴趣的:(C语言)