文件遍歷

原文鏈接:https://blog.csdn.net/akadiao/article/details/79044390

_finddata_t 結構體

struct _finddata_t 是用來存儲文件各種信息的結構體。 
定義如下:

 

struct _finddata_t
{
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[_MAX_FNAME];
};
————————————————
版權聲明:本文爲CSDN博主「阿卡蒂奧」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/akadiao/article/details/79044390

//遍歷文件夾下的所有文件

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

int main()
{
    //目標文件夾路徑
    std::string inPath = "C:\\Program Files\\*";
    //用於查找的句柄
    intptr_t handle;
    struct _finddata_t fileinfo;
    //第一次查找
    handle = _findfirst(inPath.c_str(), &fileinfo);
    if (handle == -1)
        return -1;
    do
    {
        //找到的文件的文件名
        printf("%s\n", fileinfo.name);

    } while (!_findnext(handle, &fileinfo));

    _findclose(handle);
    system("pause");
    return 0;
}

//遍歷文件夾下的所有jpg類型的文件

#include <stdio.h>
#include <io.h>
#include "string"
#include <iostream>
using namespace std;
int main()
{
    //目標文件夾路徑
    std::string inPath = "D:\\2019cloud\\NorthAfcrianGF\\*.jpg";//遍歷文件夾下的所有.jpg文件
    //用於查找的句柄
    intptr_t handle;
    struct _finddata_t fileinfo;
    //第一次查找
    handle = _findfirst(inPath.c_str(), &fileinfo);
    if (handle == -1)
        return -1;
    string path = "D:\\2019cloud\\NorthAfcrianGF\\";
    std::string pathall;
    do
    {
        //找到的文件的文件名
        printf("%s\n", fileinfo.name);
        
        std::string pathall = path+fileinfo.name;


        cout << "all   " << pathall << endl;
    } 
    while (!_findnext(handle, &fileinfo));

    cout <<"all   "<< pathall << endl;
    _findclose(handle);
    system("pause");
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章