常用函数-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 ...
随机推荐
- Java第二次作业第五题
自定义异常类,非法年龄类,并在person3类中使用此类,根据情况抛出异常,并进行处理. package naizi; class IllegalAgeException extends Except ...
- Java程序连接数据库
/** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ // 第一种实现 /** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ @Test p ...
- App引流增长技术:Deeplink(深度链接)技术
移动互联网时代,信息的分享传播无疑是 App 引流增长的关键,与其花费大量精力和成本找渠道.硬推广,不如从细节下手,用最快最简便的方法实现 Deeplink(深度链接)技术,打破信息孤岛.缩短分享路径 ...
- 第八届蓝桥杯java b组第十题
标题: k倍区间 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍 ...
- Sentinel基本概念
Sentinel是阿里开源的一款高性能的限流框架.这里将对Sentinel的使用和实现进行介绍. 这里先介绍下Sentinel中涉及到的基本概念,包括使用上或者实现上.主要是笔者在阅读文档和源码时 ...
- springboot 配置mybatis打印sql
方式 一: ###########################################################配置打印sql############################ ...
- CentOS7 搭建php环境
1,先安装apache: yum install httpd 配置ServerName,进入httpd.conf文件: vi /etc/httpd/conf/httpd.conf 将#ServerNa ...
- 【SQL server基础】objectproperty()函数
SQL Server OBJECTPROPERTY使用方法 OBJECTPROPERTY 返回有关当前数据库中的模式作用域对象的信息.此函数不能用于不是模式范围的对象,例如数据定义语言(DDL)触 ...
- CSS3 box-shadow阴影
box-shadow: color x-offset v-offset blur spred color: 阴影的颜色 x-offset: 阴影水平距离; 0: 左右阴影平分:正值:阴影在对象的右侧: ...
- DP动态规划———LCS最长公共子序列
递推公式: ]==b[j-]) { dp[i][j]=dp[i-][j-]+; } else { dp[i][j]=max(dp[i-][j],dp[i][j-]); } 完整模板代码: int LC ...