C++创建文件夹和文件夹下相关操作

1、直接上干活!

cpp 复制代码
/*****************************************************************//**
 * \file   FilesTools.h
 * \brief  
 * 
 * \author YZS
 * \date   December 2024
 *********************************************************************/
#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<iostream>
#include <string>
#include <map>
#include <io.h>
#include <vector>
#include <fstream>

#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cstdint>

#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cctype>
#include <cerrno> // errno
#include <cstring> // std::strerror
#include <cstdio> // std::rename

#if defined(_WIN32)
#   include <direct.h>
#   include <io.h>
#   include <shlobj.h>
#   include <sys/stat.h>
#   include <sys/types.h>
#else // Linux, OSX, ...
#   include <dirent.h>
#   include <unistd.h>
#   include <sys/stat.h>
#   include <sys/types.h>
#   include <pwd.h>
#endif


// getAllFiles 和 AutoGetFileName 相同
void getFiles(const std::string& path, std::vector<std::string>& files)
{
	//文件句柄
	long long hFile = 0;
	//文件信息,_finddata_t需要io.h头文件
	struct _finddata_t fileinfo;
	std::string p;
	int i = 0;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			//如果是目录,迭代之
			//如果不是,加入列表
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

//读取某给定路径下所有文件夹与文件名称,并带完整路径
void getAllFiles(std::string path, std::vector<std::string>& files)
{
	using namespace std;
	//文件句柄
	long long  hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				//文件夹
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
					files.push_back(p.assign(path).append("\\").append(fileinfo.name));
					//getFilesall(p.assign(path).append("\\").append(fileinfo.name), files);

					getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
				}
			}
			else
			{
				//文件名字
				files.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

void AutoGetFileName(std::string path, std::vector<std::string>& veflies, std::string filetype)
{
	using namespace std;
	long long hFile = 0;

	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*" + filetype).c_str(), &fileinfo)) != -1)
	{
		do
		{
			veflies.push_back(p.assign(path).append("\\").append(fileinfo.name));
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

//同理,只读取某给定路径下所有文件夹名(以下类似,只给出函数,调用案例同上):
void getJustCurrentDir(std::string path, std::vector<std::string>& files)
{
	using namespace std;
	//文件句柄
	long long  hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
					//只是包含文件夹名字
					//files.push_back(fileinfo.name);

					//getJustCurrentDir(p.assign(path).append("\\").append(fileinfo.name), files);

					//包含路径+文件夹名字
					//files.push_back(p.assign(path).append("\\").append(fileinfo.name));
					files.push_back(p.assign(path).append("\\").append(fileinfo.name));

					//扫描所有的文件夹
					getJustCurrentDir(p.assign(path).append("\\").append(fileinfo.name), files);
				}
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

/// 只读取某给定路径下的当前文件名:
void getJustCurrentFile(std::string path, std::vector<std::string>& files)
{
	using namespace std;
	//文件句柄
	long long  hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				;
			}
			else
			{
				files.push_back(fileinfo.name);
				//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}



std::string left(std::string const& str, std::size_t chars);

/** Returns the rightmost 'chars' characters of 'str'. */
std::string right(std::string const& str, std::size_t chars);

/** Determines if the given path is a directory. */
bool exists(char const* pathname);

/** Determines if the given path is a directory. */
bool dir_exists(char const* pathname);

/** Determines if the given path is a file. */
bool file_exists(char const* pathname);

/** Creates a new directory. */
int mkdir(char const* pathname/*, mode_t mode*/);

inline std::string left(std::string const& str, std::size_t chars)
{
	return str.substr(0, min(str.size(), chars));
}

inline std::string right(std::string const& str, std::size_t chars)
{
	return str.substr(str.size() - min(str.size(), chars));
}

bool
exists(char const* pathname)
{
#ifdef _WIN32
	struct _stat statbuf;
	if (::_stat(pathname, &statbuf) < 0)
		return false;
#else // _WIN32
	struct stat statbuf;
	if (::stat(pathname, &statbuf) < 0)
		return false;
#endif // _WIN32

	return true;
}

/* ---------------------------------------------------------------- */

bool
dir_exists(char const* pathname)
{
#ifdef _WIN32
	struct _stat statbuf;
	if (::_stat(pathname, &statbuf) < 0)
		return false;

	if (!(statbuf.st_mode & _S_IFDIR))
		return false;
#else // _WIN32
	struct stat statbuf;
	if (::stat(pathname, &statbuf) < 0)
		return false;

	if (!S_ISDIR(statbuf.st_mode))
		return false;
#endif // _WIN32
	return true;
}


/* ---------------------------------------------------------------- */

bool
file_exists(char const* pathname)
{
#ifdef _WIN32
	struct _stat statbuf;
	if (::_stat(pathname, &statbuf) < 0)
		return false;

	if (!(statbuf.st_mode & _S_IFREG))
		return false;
#else // _WIN32
	struct stat statbuf;
	if (::stat(pathname, &statbuf) < 0)
		return false;

	if (!S_ISREG(statbuf.st_mode))
		return false;
#endif // _WIN32

	return true;
}

int
mkdir(char const* pathname/*, mode_t mode*/)
{
#ifdef _WIN32
	if (::_mkdir(pathname) < 0)
		return 0;
#else // _WIN32
	if (::mkdir(pathname, S_IRWXU | S_IRGRP | S_IXGRP) < 0)
		return false;
#endif // _WIN32

	return 1;
}


void CreateDir(const char* dir)
{
	using namespace std;
	int m = 0, n;
	string str1, str2;
	str1 = dir;
	str2 = str1.substr(0, 2);
	str1 = str1.substr(3, str1.size());
	while (m >= 0)
	{
		m = str1.find('\\');

		str2 += '\\' + str1.substr(0, m);
		//判断该目录是否存在
		n = _access(str2.c_str(), 0);
		if (n == -1)
		{
			//创建目录文件
			_mkdir(str2.c_str());
		}

		str1 = str1.substr(m + 1, str1.size());
	}
}
相关推荐
coding随想1 小时前
JavaScript中的BOM:Window对象全解析
开发语言·javascript·ecmascript
凌肖战1 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
念九_ysl1 小时前
Java 使用 OpenHTMLToPDF + Batik 将含 SVG 遮罩的 HTML 转为 PDF 的完整实践
java·开发语言·pdf
yaoxin5211231 小时前
124. Java 泛型 - 有界类型参数
java·开发语言
liulilittle2 小时前
深度剖析:OPENPPP2 libtcpip 实现原理与架构设计
开发语言·网络·c++·tcp/ip·智能路由器·tcp·通信
88号技师2 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
勤奋的知更鸟2 小时前
Java 编程之模板方法模式
java·开发语言·模板方法模式
ゞ 正在缓冲99%…2 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
十年编程老舅3 小时前
跨越十年的C++演进:C++20新特性全解析
c++·c++11·c++20·c++14·c++23·c++17·c++新特性
上单带刀不带妹3 小时前
手写 Vue 中虚拟 DOM 到真实 DOM 的完整过程
开发语言·前端·javascript·vue.js·前端框架