常用函数-Linux文件操作
/************************************************************************
函数功能:寻找文件夹下的某格式文件
std::vector<string> &filelist -- 文件名list
const char *basePath -- 文件路径
string format -- 文件格式 如 .xml
************************************************************************/
int readFileList(std::vector<string> &filelist, const char *basePath, string format)
{
DIR *dir;
struct dirent *ptr;
char base[];
if ((dir = opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit();
}
while ((ptr = readdir(dir)) != NULL)
{
if (strcmp(ptr->d_name, ".") == || strcmp(ptr->d_name, "..") == ) ///current dir OR parrent dir
continue;
else if (ptr->d_type == ) //file
{
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
string temp = ptr->d_name;
//cout << temp << endl;
string sub = temp.substr(temp.length() - , temp.length() - );
//cout << sub << endl;
if (sub == format)
{
//string path = basePath;
//path += "/";
//path += ptr->d_name;
filelist.push_back(ptr->d_name);
}
}
else if (ptr->d_type == ) ///link file
{
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
}
else if (ptr->d_type == ) ///dir
{
memset(base, '\0', sizeof(base));
strcpy(base, basePath);
strcat(base, "/");
strcat(base, ptr->d_name);
readFileList(filelist, base, format);
}
}
closedir(dir);
return ;
}
/************************************************************************
函数功能:文件夹内文件拷贝到另一文件夹
std::vector<string> &filelist -- 文件名list
std::string& srcDirPath -- 源路径
std::string& desDirPath -- 目的路径
************************************************************************/
void do_copy(const std::vector<std::string> &fileNameList, std::string& srcDirPath, std::string& desDirPath)
{
for (int i = ; i < fileNameList.size(); i++)
{
std::string nowSrcFilePath, nowDesFilePath;
nowSrcFilePath = srcDirPath + "/" + fileNameList.at(i);
nowDesFilePath = desDirPath + "/" + fileNameList.at(i);
std::ifstream in;
in.open(nowSrcFilePath);
if (!in)
{
std::cout << "open src file : " << nowSrcFilePath << " failed" << std::endl;
continue;
}
std::ofstream out;
out.open(nowDesFilePath);
if (!out)
{
std::cout << "create new file : " << nowDesFilePath << " failed" << std::endl;
in.close();
continue;
}
out << in.rdbuf();
out.close();
in.close();
}
}
/************************************************************************
函数功能:删除某路径下的文件
std::vector<string> &filelist -- 文件名list
std::string& srcDirPath -- 路径
************************************************************************/
void do_delete(const std::vector<std::string> &fileNameList, std::string& srcDirPath)
{
for (int i = ; i < fileNameList.size(); i++)
{
std::string nowSrcFilePath;
nowSrcFilePath = srcDirPath + "/" + fileNameList.at(i);
remove(nowSrcFilePath.c_str());
}
}
//*************************************************************************
// 函数名称: get_app_path
// 返 回 值: const std::string&
// 参 数: const char * init
// 函数说明: 可以把main函数的第一个参数传进init,以它的路径初始化app_path
// 但要注意init参数在第一次调用get_app_path时有效,否则无效
//*************************************************************************
const std::string& get_app_path(const char* init)
{
static std::string app_path;
if (app_path.empty())
{
if (NULL != init)
{
app_path = init;
return app_path;
} char path[] = { '\0' };
//获取当前目录绝对路径
if (NULL == getcwd(path, sizeof(path)))
{
printf("***Error***\n");
}
app_path = path;
}
return app_path;
}
常用函数-Linux文件操作的更多相关文章
- Linux 文件操作接口
目录 Linux 文件操作接口 C语言文件操作接口 C语言文件描述 fopen() r模式打开文件 w模式打开文件 a模式打开文件 其他模式类似 fclose() fwrite() fread() 系 ...
- [C#] 常用工具类——文件操作类
/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...
- ansible笔记(5):常用模块之文件操作(二)
ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...
- C/C++以及Linux文件操作备忘录
目录 C文件操作 文件开关 文件读写 C++文件操作 Linux文件操作 打开 C文件操作 #include<stdio.h> stdin, stdout, stderr 文件开关 /* ...
- 归纳整理Linux下C语言常用的库函数----文件操作
在没有IDE的时候,记住一些常用的库函数的函数名.参数.基本用法及注意事项是很有必要的. 参照Linux_C_HS.chm的目录,我大致将常用的函数分为一下几类: 1. 内存及字符串控制及操作 2. ...
- Linux文件操作常用命令整理
收集.整理日常系统管理或维护当中的,常用到的一些关于文件操作的命令或需求,后续会慢慢补充.完善! 查看.生成指定目录的目录树结构? [root@DB-Server ~]#tree #当前目录 ...
- linux常用命令之------文件操作、文件查看、权限、打包压缩
1.一般公司把linux作为自己的应用服务器,将应用和服务器部署在上面 2.测试一般用来打包.压缩.查日志,写一个简单的shell 获得linux服务器的方式 a:网上租一台云服务器 b:安装vmwa ...
- Linux文件操作常用命令
一.一些文件操作命令. 1.cd /home 进入"home目录" 2.cd ../ 返回上一级目录 3.cd - 返回上次所在的目录 4.pwd 显示工程路径 5.ll 显示 ...
- C语言的fopen函数(文件操作/读写)
头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为: FILE * fopen(const char * path, c ...
随机推荐
- C++11新增容器以及元组
上次说了C++11的部分新特性,这里我们来说说新增的容器. unordered_map unordered_set unordered_multimap unordered_multiset arra ...
- 基于ViewPager与TabLayout建立三类图表
延续昨天,今天使用ViewPager和TabLayout来实战一下,顺便补充一下新知识: 1.线形图,显示一周的温度情况. 2.饼状图,2017年互联网教育细分领域投资情况. 3.柱状 ...
- H2 数据库使用简介
博客地址:http://www.moonxy.com 一.前言 H2 是一个用 Java 开发的嵌入式数据库,它本身只是一个类库,即只有一个 jar 文件,可以直接嵌入到应用项目中.H2 主要有如下三 ...
- Python学习-字符编码, 数据类型
本篇主要内容: 字符编码 Python中的数据类型有哪些 类型的一些常用操作及方法 一.字符编码 编码解释的大部分内容摘自廖雪峰老师教程中的讲解,点击跳转. 简单介绍: 我们知道计算机只能处理数字,如 ...
- shell 获取当前目录下的jar文件
1.示例 function getDir() { ` do fileName=$"/"$item if [ -d $fileName ] then echo $fileName&q ...
- springboot 2.1.3 + mybatis + druid配置多数据源
在一些大型的项目中,通常会选择多数据库来满足一些业务需求,此处讲解使用springboot.mybatis和druid来配置多数据源 1.依赖配置 pom文件引入相关依赖 <dependency ...
- Scanner类的next()方法和nextLine()方法的异同点
通过一段代码就可以明白其中的奥妙!! import java.util.Scanner; public class next_nextLine { public static void main(St ...
- 洛谷:P3950 部落冲突
原题地址:https://www.luogu.org/problemnew/show/P3950 题目简述 给定一棵树,每次给定一个操作,有如下两种: 将某条边染黑 2.询问给定的u,v两点间是否有边 ...
- 面试官:你有m个鸡蛋,如何用最少的次数测出鸡蛋会在哪一层碎?
假设你面前有一栋n层的大楼和m个鸡蛋,假设将鸡蛋从f层或更高的地方放扔下去,鸡蛋才会碎,否则就不会.你需要设计一种策略来确定f的值,求最坏情况下扔鸡蛋次数的最小值. leetcode原题链接 乍一看这 ...
- 基于SpringBoot+WebSocket搭建一个简单的多人聊天系统
前言 今天闲来无事,就来了解一下WebSocket协议.来简单了解一下吧. WebSocket是什么 首先了解一下WebSocket是什么?WebSocket是一种在单个TCP连接上进行全双工 ...