当前位置:   article > 正文

c/c++遍历文件夹_遍历目录文件 c++

遍历目录文件 c++

前言

本文记录了使用c++来实现遍历文件的一些实现方法,并在Windows和Linux进行了验证。

Windows遍历文件夹

使用运行库

示例代码

#include <iostream>
#include <io.h>
using namespace std;

int PrintFileList(const char *path)
{
   if (!path) return 0;
   int len = strlen(path) + 3;
   char *buffer = new char[len];
   sprintf(buffer, "%s\\*", path);
   _finddata_t info;
   intptr_t handle = _findfirst(buffer, &info);//返回-1表示失败
   delete[] buffer;
   if (handle == -1) {
       return 0;
   }
   int cnt = 0;
   do
   {
       //过滤掉2个特殊的文件
       //. 表示当前目录 
       //..表示上一级目录
       if (strcmp(info.name, ".") == 0
           || strcmp(info.name, "..") == 0) {
           continue;
       }
       if (info.attrib & _A_SUBDIR) {
           //如果是文件夹,进行递归遍历
           len = strlen(path) + strlen(info.name) + 2;
           buffer = new char[len];
           sprintf(buffer, "%s\\%s", path, info.name);
           cnt += PrintFileList(buffer);
           delete[] buffer;
       }
       else {
           //这里只打印出文件的绝对路径
           cout << path << "\\" << info.name << endl; 
           ++cnt;
       }
   } while (_findnext(handle, &info) == 0);
   _findclose(handle);
   return cnt;
}

int main()
{
   int n = PrintFileList(".");
   cout << "文件个数:" << n << endl;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

使用Win32 API函数

示例代码:

#include <iostream>
#include <Windows.h>
using namespace std;

int PrintFileList(const TCHAR *path)
{
   if (!path) return 0;
   int len = lstrlen(path) + lstrlen(TEXT("\\*")) + 1;
   TCHAR *buffer = new TCHAR[len];
   lstrcpy(buffer, path);
   lstrcat(buffer, TEXT("\\*"));
   WIN32_FIND_DATA info;
   HANDLE handle = FindFirstFile(buffer, &info);
   delete[] buffer;
   if (handle == INVALID_HANDLE_VALUE) {
       return 0;
   }
   int cnt = 0;
   do
   {
       if (lstrcmp(info.cFileName, TEXT(".")) == 0
           || lstrcmp(info.cFileName, TEXT("..")) == 0) {
           continue;
       }
       if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
           len = lstrlen(path) + lstrlen(info.cFileName) + lstrlen(TEXT("\\")) + 1;
           buffer = new TCHAR[len];
           lstrcpy(buffer, path);
           lstrcat(buffer, TEXT("\\"));
           lstrcat(buffer, info.cFileName);
           cnt += PrintFileList(buffer);
           delete[] buffer;
       }
       else {
#ifdef _UNICODE
           wcout << path << "\\" << info.cFileName << endl;
#else
           cout << path << "\\" << info.cFileName << endl;
#endif
           ++cnt;
       }
   } while (FindNextFile(handle, &info));
   FindClose(handle);
   return cnt;
}

int main()
{
   int n = PrintFileList(TEXT("."));
   cout << "文件个数:" << n << endl;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

Linux遍历文件夹

示例代码:

#include <iostream>
#include <string.h>
#include <dirent.h>
using namespace std;

int PrintFileList(const char *path)
{
   DIR *dir = opendir(path);
   if (!dir)
       return 0;

   int cnt = 0;
   dirent *dirent;
   while ((dirent = readdir(dir)))
   {
       if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
       {
           continue;
       }
       /*
       DT_FIFO = 1, 管道
       DT_CHR = 2, 字符设备
       DT_DIR = 4,  文件夹
       DT_BLK = 6,  块设备
       DT_REG = 8,  文件
       DT_LNK = 10,  链接
       DT_SOCK = 12, 本地套接字
       DT_WHT = 14
       */
       if (dirent->d_type & DT_DIR)
       {
           int len = strlen(path) + strlen(dirent->d_name) + 2;
           char *buffer = new char[len];
           sprintf(buffer, "%s/%s", path, dirent->d_name);
           cnt += PrintFileList(buffer);
           delete [] buffer;
       }
       else
       {
           cout << path << "/" << dirent->d_name << endl;
           ++cnt;
       }
   }

   closedir(dir);
   return cnt;
}

int main()
{
   int n = PrintFileList(".");
   cout << "文件个数:" << n << endl;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

C++17遍历文件夹

该方法使用标准库,理论上和平台无关,但是需要支持C++17的编译器才能编译通过。
示例代码:

#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;

int PrintFileList(const fs::path& path)
{
	int n = 0;
	fs::directory_iterator dir(path);
	for (const auto& it : dir)
	{
		if (it.is_directory())
		{
			n += PrintFileList(it.path());
		}
		else
		{
			cout << it.path().string() << endl;
			++n;
		}
	}
	return n;
}

int main()
{
	int n = PrintFileList(".");
	cout << "文件个数:" << n << endl;
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  1. g++命令,添加选项参数-std=c++17
  2. vs工程配置, 匹配属性=>属性=>常规=>C++语言标准=>ISO C++17标准(/std:c++17)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/716069
推荐阅读
相关标签
  

闽ICP备14008679号