/************************************************************************
函数功能:寻找文件夹下的某格式文件
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. 记录工作中linux相关操作

    在项目部署之后,查看日志能查看部署结果是否正确部署. 最开始查看日志我会使用cat service.log tail -f service.log vim service.log 打开日志之后 /+查 ...

  2. Dart函数、类和运算符-处理信息

    编程语言虽然千差万别,但归根结底,它们的设计思想无非就是回答两个问题: 1.如何表示信息: 2.如何处理信息: 函数 函数是一段用来独立地完成某个功能的代码.函数是对象类型,它的类型叫做Functio ...

  3. axios使用post方式请求出现400

    在vue中如果直接使用与ajax的参数传递方式类型是不正确的,需要转化,方式有两种 1:使用 new URLSearchParams()构造参数 let params = new URLSearchP ...

  4. springboot应用在tomcat中运行

    1.将打包方式改成war,因为如果是java -jar xx.jar方式运行,一定是jar包 <packaging>war</packaging> 2.添加tomcat的依赖, ...

  5. vmware配置静态ip

    wmware安装后,默认是动态ip地址. 在测试环境搭建虚拟机后,都需要使用静态ip地址.但是配置固定静态ip地址后,虚拟机总是不能上网和访问网站域名. 原来问题出在配置固定ip后配置的的网关和域名解 ...

  6. CentOS 7上编写自定义系统审计规则

    1)简介 Linux审计系统创建审计跟踪,这是一种跟踪系统上各种信息的方法.它可以记录大量数据,如事件类型,日期和时间,用户ID,系统调用,进程,使用的文件,SELinux上下文和敏感度级别.它可以跟 ...

  7. Google Test入门教程:从下载到运行

    本文以VS2019为例,自己的工程使用Debug x64,多线程调试DLL(/MDd),用户可以根据自己需求更改配置,只要所有配置前后统一即可. 第一步:clone Google Test源码 打开h ...

  8. 2017CCSP-01 五子棋 简单模拟

    题面和数据 对于每个空格,先定义一个变量cnt=0,表示空格填充后对五子数量的变化,四个方向进行两边搜索,设对于每个方向连续的白棋子分别为\(wa\),\(wb\),有以下几种情况. \(wa> ...

  9. 1 Processing入门简介

    1 Processing入门简介 1.1 Before you start Processing是一个为开发面向图形的应用(visually oriented application)而生的简单易用的 ...

  10. Yii2.0怎么设置时区?

    在config文件夹下,main.php 中, return [ 'charset' => 'utf-8', 'language' => 'zh-CN', 'timeZone' => ...