zoukankan      html  css  js  c++  java
  • 使用C/C++遍历文件夹

    一个遍历一个文件夹下文件名的方法:

    首先使用FindFirstFile()和FindNextFile()这两个函数获得文件夹下文件的名称,再做其他的处理。

    使用windows.h中的函数FindFirstFILE() 和FindNextFile()遍历文件夹中的函数。

    //wchar to char
    void W2C(char *ch, const WCHAR *wch)
    {
        int i = 0;
        for (; wch[i]; i++)
        {
            ch[i] = wch[i];
        }
    
        ch[i] = 0;
    }
    
    //char to wchar
    void C2W(WCHAR *wch, const char *ch)
    {
        int i = 0;
        for (; ch[i]; i++)
        {
            wch[i] = ch[i];
        }
    
        wch[i] = 0;
    }
    
    //更改  后缀 .jpg   可以获得不同文件的名称
    bool GetJpegPicsNameList(const string &filePath, vector<string>& fileNameSet)
    {
        assert(filePath[filePath.length() - 1] == '\');
        WIN32_FIND_DATA file_info;
        static HANDLE h;
        WCHAR wchFilePath[99] = { 0 };
        char chFileName[99] = { 0 };
        
    
        C2W(wchFilePath, filePath.c_str());
        if (wchFilePath[wcslen(wchFilePath) - 1] == '\')wcscat_s(wchFilePath, L"*.jpg");
        else wcscat_s(wchFilePath, L"\*.jpg");
    
        h = FindFirstFile(wchFilePath, &file_info);
        if (h == INVALID_HANDLE_VALUE)      //if handle h is invalid
        {
            cout << "can't open this file or dir :%s  ,or there is no *.jpg files
     press any key to exit!" << filePath;
            return FALSE;
        }
        else
        {
            W2C(chFileName, file_info.cFileName);
            fileNameSet.push_back(filePath + chFileName);
        }
    
        while (FindNextFile(h, &file_info))
        {
            W2C(chFileName, file_info.cFileName);
            fileNameSet.push_back(filePath + chFileName);
        }
        
        return TRUE;
    }
    获得文件夹下所有jpg图片的名称(名称中包含路径)

    函数说明:

    1、FindFirstFile()   //#include<windows.h>

    HANDLE WINAPI  FindFirstFile(   //返回值是句柄 HANDLE,这个值用于函数FindNextFile()
      _In_   LPCTSTR lpFileName,  //需要遍历的文件夹的路径名,此参数不能为空,不能为空字符串,
                                                                                                                //最好使用通配符来限定查找的文件,例如“C:\Jiahu\*.jpg”
      _Out_  LPWIN32_FIND_DATA lpFindFileData  //结构体lp..Data用于存储找到的文件的信息,
                                                                                                               //其中lpFindFileData.cFileName存储文件的文件名(ANSIC)
    );
    
    If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile orFindClose, and the lpFindFileData parameter contains information about the first file or directory found.
    
    如果函数执行成功,则返回一个用于函数FindNextFile()或FindClose()的句柄。参数 lpFindFileData 用于存储查找到的第一个文件的信息。
    
    If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value isINVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate. 
    
    如果函数执行失败,其返回值为 INVALID_HANDLE_VALUE ,而且参数lpFindFileData 中的数据无效。
    
     
    FindFirstFile()

    2、FindNextFile() //#include<windows.h> 

    BOOL WINAPI FindNextFile( //找到下一个函数返回TRUE否则返货FALSE
      _In_   HANDLE  hFindFile,  //FindFirstFile()执行成功所返回的句柄
      _Out_  LPWIN32_FIND_DATA lpFindFileData //用于存储找到的文件的信息,可以与FindFirstFile()公用同一个参数。
    );
    
    If the function succeeds, the return value is nonzero and the lpFindFileData parameter contains information about the next file or directory found.
    
    如果函数执行成功,返回非零并且参数lpFindFileData 存储了文件的信息。
    
    If the function fails, the return value is zero and the contents of lpFindFileData are indeterminate. 
    
    如果函数执行失败,其返回值是0并且参数lpFindFileData 无效。
    FindNextFile()

     

    下面的函数我用于将整个文件夹下的jpg文件进行更名的,就是将jpg文件的前缀改为相同。最后文件的名称像:blue_0.jpg  blue_1.jpg  blue_2.jpg .....blue_99.jpg...

    下面代码中使用的方法不好,应该先获得文件夹中所有jpg文件的名称再对这些图片进行更名,这样写的代码思路更清晰。

    #include<stdio.h>
    #include<stdlib.h>
    #include<windows.h>
    
    void ReName(char *file_name , char *dir_path);//用于更改整个文件夹下文件的名称,file_name为文件名的前缀。像 blue...
    
    int main()
    {
    printf("Enter prefix :");
    char prefix[9];
    scanf("%s" , prefix);
    ReName(prefix , ".");//点 “.”  表示当前目录下
    return 0;
    }
    
    void ReName(char *File_name ,char *Dir_path)
    {
    WIN32_FIND_DATA dir;  //定义dir用于存储找到的文件的信息。用于函数FindFirstFile()和FindNextFile()
    
    HANDLE h = FindFirstFile(Dir_path , &dir) ;
    if(h == INVALID_HANDLE_VALUE)      //若句柄h无效效
    {
    printf("can't open this file or dir :%s
     press any key to exit!" , Dir_path);
    getchar();
    exit(0);
    }
    else        //句柄h有效
    {
    char new_name[21];                  //文件的新名称
    strcpy(new_name , File_name);//所有的文件使用相同的前缀 file_name
    strcat(new_name , "0.jpg");     //第一个文件明明为file_name0.jpg
    int i =0;                                   //为余下的文件计数
    char Cnt[6];                                          
    rename(dir.cFileName , new_name);//更改第一个找到的文件名
    while(FindNextFile(h , &dir))//更改余下的文件的名称
    {
    i++;                                        //文件计数
    itoa(i , Cnt ,10);                       //int转字符串 
    strcpy(new_name , File_name);//下面3句生成新文件名
    strcat(new_name , Cnt);
    strcat(new_name , ".jpg");
    rename(dir.cFileName , new_name);//文件更名
    }
    }
    }
    ReName()

     

    #include<stdio.h>
    #include<stdlib.h>
    #include<windows.h>
    
    static int GetAJPEGName(char *file_path , char *file_name);//if there is a new file ,return 1 and save name in file_name
    
    static int GetAJPEGName(char *file_path , char *file_name)
    {
        static int flag = 0;
        flag++;// flag used for count how many times this function has been called
    
        char Jpeg_path[256];//just get name of  *.jpg file
        strcpy(Jpeg_path , file_path);
        if(Jpeg_path[strlen(Jpeg_path)-1] == '\')strcat(Jpeg_path , "*.jpg");
        else strcat(Jpeg_path , "\*.jpg");
    
        WIN32_FIND_DATA file_info;
        static HANDLE h;
    
        if(flag == 1) // if this function is called for the first time ,use function  FindFirstFile()
        {
            h = FindFirstFile(Jpeg_path , &file_info);
            if(h == INVALID_HANDLE_VALUE)      //if handle h is invalid
            {
                printf("can't open this file or dir :%s ,or there is no *.jpg files
     press any key to exit!" , file_path);
                return 0;
            }
            else//if h is valid
            {
                strcpy(file_name , file_info.cFileName);
                return 1;
            }
        }
        else //if this function is called more than one times,use function FindNextFile()
        {
            if(FindNextFile(h , &file_info))//if there is a new file
            {
                strcpy(file_name , file_info.cFileName);
                return 1;
            }
            else//no more files
            {
                return 0;
            }
        }
    }
    windows:GetAJPEGName
  • 相关阅读:
    python数据分析实例(1)
    Latex学习(1): longtable的分页显示保留表头与标题L
    pl/sql学习(5): 触发器trigger/事务和锁
    pl/sql学习(6): 引号/程序调试/列中的字符串合并/正则表达式
    pl/sql学习(4): 包package
    SQL入门(4): 嵌入式SQL语言
    我觉得vue原理就是 Object.defineProperty,简单实现了set和get,谢谢
    vue组件一
    css3 flex盒子布局
    php连接数据库、创建数据库、创建数据表
  • 原文地址:https://www.cnblogs.com/jiahu-Blog/p/4383263.html
Copyright © 2011-2022 走看看