/************************************************************************
函数功能:寻找文件夹下的某格式文件
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. PythonI/O进阶学习笔记_6.对象引用,可变性和垃圾回收

    前言: 没有前言了- -......这系列是整理的以前的笔记上传的,有些我自己都忘记我当时记笔记的关联关系了. 记住以后 笔记记了就是用来复习的!!!不看不就啥用没了吗!!! content: 1.p ...

  2. Session的创建和设置

    1.Session的获取: (1)无参的方法: protected void doGet(HttpServletRequest request, HttpServletResponse respons ...

  3. ASP.NET Core 2.2 : 二十三. 深入聊一聊配置的内部处理机制

    上一章介绍了配置的多种数据源被注册.加载和获取的过程,本节看一下这个过程系统是如何实现的.(ASP.NET Core 系列目录) 一.数据源的注册 在上一节介绍的数据源设置中,appsettings. ...

  4. Linux系统在开机的时候自动启动SVN

    Linux系统在开机的时候自动启动SVN 1.创建执行脚本svn.sh(/root路径下,随便哪个路径),其内容很简单,如下: #!/bin/bash     svnserve -d --listen ...

  5. 【linux】【ELK】利用elasticproxy对elasticsearch进行二次排序

    做过elk的人应该了解kibana排序至支持到秒级别,但同一秒内出现多个日志的时候那么kibana展示的日志就会混轮,加上该代理可以解决该问题 # 拉取elasticproxy镜像 [root@loc ...

  6. Transformer各层网络结构详解!面试必备!(附代码实现)

    1. 什么是Transformer <Attention Is All You Need>是一篇Google提出的将Attention思想发挥到极致的论文.这篇论文中提出一个全新的模型,叫 ...

  7. lvm创建逻辑卷技巧

    公司使用的服务器都是虚拟机,是虚拟机管理员通过模板创建的. 创建的所有逻辑卷都是使用的sda盘. 而我们在部署应用时需要和系统所在盘分离.(提高磁盘读写速度,避免系统盘被占满) 以前都是先创建新的逻辑 ...

  8. Unity项目 - DeathtrapDungeon死亡地牢

    目录 游戏原型 项目演示 绘图资源 代码实现 注意事项 技术探讨 参考来源 游戏原型 死亡地牢是一款 2D-Roguelike 的地牢冒险游戏.手握利刃,斩杀怪物,在凶险的地牢内生存下去.但注意,敌人 ...

  9. 解决CentOS6.x或RedHat Linux 6.x版本不能通过System eth0以固定IP访问外网的问题

    当你在VMware Workstation Pro中,打开从别人那里克隆来的系统,或者是开启迁移后的虚拟机系统时,VMware将会提示你:此虚拟机可能已被移动或 复制.为了配置特定的管理和网络功能.V ...

  10. 记录ceph两个rbd删除不了的处理过程

    在一个使用的环境发现两个ceph的rbd删除不了,发现两个rbd都是由于残留了watch的信息.在此记录处理过程. 处理方法 [root@node- ~]# rbd -4cce-b39d-709e05 ...