/************************************************************************
函数功能:寻找文件夹下的某格式文件
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文件操作的更多相关文章

  1. Linux 文件操作接口

    目录 Linux 文件操作接口 C语言文件操作接口 C语言文件描述 fopen() r模式打开文件 w模式打开文件 a模式打开文件 其他模式类似 fclose() fwrite() fread() 系 ...

  2. [C#] 常用工具类——文件操作类

    /// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...

  3. ansible笔记(5):常用模块之文件操作(二)

    ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...

  4. C/C++以及Linux文件操作备忘录

    目录 C文件操作 文件开关 文件读写 C++文件操作 Linux文件操作 打开 C文件操作 #include<stdio.h> stdin, stdout, stderr 文件开关 /* ...

  5. 归纳整理Linux下C语言常用的库函数----文件操作

    在没有IDE的时候,记住一些常用的库函数的函数名.参数.基本用法及注意事项是很有必要的. 参照Linux_C_HS.chm的目录,我大致将常用的函数分为一下几类: 1. 内存及字符串控制及操作 2. ...

  6. Linux文件操作常用命令整理

    收集.整理日常系统管理或维护当中的,常用到的一些关于文件操作的命令或需求,后续会慢慢补充.完善! 查看.生成指定目录的目录树结构?   [root@DB-Server ~]#tree   #当前目录 ...

  7. linux常用命令之------文件操作、文件查看、权限、打包压缩

    1.一般公司把linux作为自己的应用服务器,将应用和服务器部署在上面 2.测试一般用来打包.压缩.查日志,写一个简单的shell 获得linux服务器的方式 a:网上租一台云服务器 b:安装vmwa ...

  8. Linux文件操作常用命令

    一.一些文件操作命令. 1.cd /home  进入"home目录" 2.cd ../ 返回上一级目录 3.cd -  返回上次所在的目录 4.pwd 显示工程路径 5.ll 显示 ...

  9. C语言的fopen函数(文件操作/读写)

    头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为:    FILE * fopen(const char * path, c ...

随机推荐

  1. 垃圾佬的旅游III(Hash + 暴力)

    题目链接:http://120.78.128.11/Problem.jsp?pid=3445 最开始的思路就是直接暴力求解,先把所有的数值两两存入结构体,再从小到大枚举.用二分的思路去判断数值以及出现 ...

  2. 新建web工程

    1.选择新建Dynamic  Web Project 2.选择服务器和版本(2.5) 3.WebContend目录下新建一个html文件 4.运行 工程的目录结构: WEB-INF目录时受保护的,不能 ...

  3. [Leetcode][动态规划] 第935题 骑士拨号器

    一.题目描述 国际象棋中的骑士可以按下图所示进行移动:                           我们将 “骑士” 放在电话拨号盘的任意数字键(如上图所示)上,接下来,骑士将会跳 N-1 步 ...

  4. C# 代码往oracle数据库添加datetime格式列

    C# 代码往oracle数据库添加datetime格式列时,不需要在insert语句中为datetime类型使用to_date函数

  5. 使用mkfs.ext4格式化大容量磁盘

    使用mkfs.ext4默认参数格式化磁盘后,发现格式化时间特别长,并且格式化会占用磁盘很大的空间.例如2TB的磁盘格式化会占用10分钟左右时间,并占用30G左右的磁盘空间.究其原因,原来inode会占 ...

  6. SpringBoot系列——ElasticSearch

    前言 本文记录安装配置ES环境,在SpringBoot项目中使用SpringData-ElasticSearch对ES进行增删改查通用操作 ElasticSearch官网:https://www.el ...

  7. 利用threading模块开线程

    一多线程的概念介绍 threading模块介绍 threading模块和multiprocessing模块在使用层面,有很大的相似性. 二.开启多线程的两种方式 1.创建线程的开销比创建进程的开销小, ...

  8. 关于Python selenium实现类似比价软件的功能

    偶然间想实现比价的功能,正常requests途径比较难实现,于是乎想到可以selenium可以简易实现,下面是代码. import requests from selenium import webd ...

  9. 触电JavaScript-如何将json 二维数组转换为 JSON object

    最近因为项目中使用的是 ActiveReports .Net 产品,因为他们最近新出了  ActiveReports JS 版本,所以内心有点痒痒,想试试这个纯前端版本报表控件到底如何,毕竟我们项目有 ...

  10. [Note] 使用Code Snippet简化编码

    使用NewtonSoft.Json写实体类时大量格式一致的代码出现 ,这时可以使用Code snippet来加快编码速度 [JsonProperty(PropertyName = "mess ...