C语言结合windowsApi遍历文件

<!-- lang: cpp -->
#include "stdio.h"
#include "string.h"
#include "windows.h"
#include "cv.h"
#include "highgui.h"

#define LEN 1024

char g_outFileName[100];
// 深度优先递归遍历目录中所有的文件
BOOL  DirectoryList(LPCSTR Path, char (*files)[50], int *len)
{
    WIN32_FIND_DATA FindData;
    HANDLE hFind;
    static int FileCount = 0;
    char FullPathName[LEN];
    char FilePathName[LEN];
    // 构造路径
    strcpy(FilePathName, Path);
    strcat(FilePathName, "\\*.*");
    //找到要查找的目录资源
    hFind = FindFirstFile(FilePathName, &FindData);
    if (hFind == INVALID_HANDLE_VALUE)
    {
        printf("搜索失败!");
        return 0;
    }
    //遍历找到目录下的文件
    while(::FindNextFile(hFind, &FindData))
    {
        // 过虑.和..
        if (strcmp(FindData.cFileName, ".") == 0 
            || strcmp(FindData.cFileName, "..") == 0 )
        {
            continue;
        }

        // 构造完整路径
        wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);

        // 输出本级的文件printf("\n%d  %s  ", FileCount, FullPathName);
        // 保存到数组
        strcpy(files[FileCount],FindData.cFileName);        //仅保存的文件名,故需要存放在一个目录下面
        FileCount++;
        *len = FileCount;   //数组长度

        if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            printf("<Dir>");
            DirectoryList(FullPathName,files,len);
        }

    }
    return 0;
}

int main( int argc, char** argv ) {
    char files[300][50];
    int len;
    DirectoryList("E:\\项目\\PupilDetect\\video\\xvid\\Result", files, &len);
    printf("len %d\n", len );
    for(int i=0; i<len; i++) {
        IplImage* img = cvLoadImage(files[i]);
        //圆心
        cvCircle( img, cvPoint(100,100), 2, CV_RGB(255,255,255), -1, 8, 0 );
        //半径
        cvCircle(img,cvPoint(100,100),30,cvScalar(255,255,255),2,8,0);
        sprintf(g_outFileName, "F:\\test\\%.5d%s", ++i, ".jpg");//保存的图片名
        if(!cvSaveImage(g_outFileName,img)) printf("Could not save: %s\n", g_outFileName);
        cvReleaseImage(&img);
    }

    // wait for a key
    cvWaitKey(0); 

    return 0;
}

你可能感兴趣的:(c)