zoukankan      html  css  js  c++  java
  • 采用标准c进行目录文件遍历

    图像处理的时候经常需要对一个目录的所有图像进行处理,遍历文件得c代码:

    在windows中需要使用到宽字符。

    另外,可以使用opencv封装的目录访问操作,下次给出。


    // DirTraverse.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "windows.h"
    #include <string.h>
    #include <Strsafe.h>
    #include <iostream>
    
    
    using namespace std;
    
    char *w2c(char *pcstr,const wchar_t *pwstr, size_t len);
    void TraverseDirectory(wchar_t Dir[MAX_PATH]);
    char * imageDir = "D:\enroll\";
    
    
    int main( int argc, const char** argv )
    {
    	locale loc( "chs" );                //支持中文输出,否则wchar可能无法输出值为中文的变量
    	wcout.imbue( loc );
    
    	TraverseDirectory(L"D:\images\all_0407\");
    
    	system("pause");
        return 0;
    }
    
    
    
    //Converting a WChar string to a Ansi string  
    char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)  
    {  
        int nlength=wcslen(pwstr);  
        //获取转换后的长度  
        int nbytes = WideCharToMultiByte( 0, 0, pwstr, nlength, NULL,0,NULL, NULL );   
        if(nbytes>len)   nbytes=len;  
        // 通过以上得到的结果,转换unicode 字符为ascii 字符  
        WideCharToMultiByte( 0,0, pwstr, nlength,   pcstr, nbytes, NULL,   NULL );  
        return pcstr ;  
    }  
    
    //传入要遍历的文件夹路径,并遍历相应文件夹
    void TraverseDirectory(wchar_t Dir[MAX_PATH])    
    {
    	WIN32_FIND_DATA FindFileData;
    	HANDLE hFind=INVALID_HANDLE_VALUE;
    	wchar_t DirSpec[MAX_PATH];                  //定义要遍历的文件夹的目录
    	DWORD dwError;
    	StringCchCopy(DirSpec,MAX_PATH,Dir);
            StringCchCat(DirSpec,MAX_PATH,TEXT("\*"));   //定义要遍历的文件夹的完整路径*
    
    	hFind=FindFirstFile(DirSpec,&FindFileData);          //找到文件夹中的第一个文件
    
    	if(hFind==INVALID_HANDLE_VALUE)                               //如果hFind句柄创建失败,输出错误信息
    	{
    		FindClose(hFind); 
    		return;  
    	}
    	else 
    	{
    		while(FindNextFile(hFind,&FindFileData)!=0)                            //当文件或者文件夹存在时
    		{
    			if((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0&&wcscmp(FindFileData.cFileName,L".")==0||wcscmp(FindFileData.cFileName,L"..")==0)        //判断是文件夹&&表示为"."||表示为"."
    			{
    				 continue;
    			}
    			if((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0)      //判断如果是文件夹
    			{
    				wchar_t DirAdd[MAX_PATH];
    				StringCchCopy(DirAdd,MAX_PATH,Dir);
    				StringCchCat(DirAdd,MAX_PATH,TEXT("\"));
    				StringCchCat(DirAdd,MAX_PATH,FindFileData.cFileName);       //拼接得到此文件夹的完整路径
    				TraverseDirectory(DirAdd);                                  //实现递归调用
    			}
    			if((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)==0)    //如果不是文件夹
    			{
    				//wcout<<Dir<<"\"<<FindFileData.cFileName<<endl;            //输出完整路径
    				char * fname = (char*)malloc(sizeof(char)*(2*wcslen(FindFileData.cFileName)+1));
    				memset(fname,0,2*wcslen(FindFileData.cFileName)+1);
    				w2c(fname, FindFileData.cFileName, 2*wcslen(FindFileData.cFileName)+1); // 获取文件名
    				cout<<fname<<endl;
    
    				if(strstr(fname, ".jpg"))
    				{
    					char * fullName = (char*)malloc(strlen(imageDir)+strlen(fname)+1);
    					strcpy(fullName, imageDir);
    					strcat(fullName, fname); // 获取完全路径
    					// process
    					// TODO
    					//
    					free(fullName);
    				}
    				// free
    				free(fname);
    			}
    		}
    		FindClose(hFind);
    	}
    }
    


  • 相关阅读:
    codeforces 407B Long Path
    CodeForces 489C Given Length and Sum of Digits...
    hacker cup 2015 Round 1 解题报告
    hacker cup 2015 资格赛
    Codeforces 486(#277 Div 2) 解题报告
    POJ 3468 A Simple Problem with Integers splay
    Codeforces 484(#276 Div 1) D Kindergarten DP
    求平均值问题201308031210.txt
    I love this game201308022009.txt
    QQ
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3167761.html
Copyright © 2011-2022 走看看