linux文件管理
来源:互联网 发布:快乐拍软件 编辑:程序博客网 时间:2024/06/05 09:38
//判断是否为目录
bool is_dir(const char *path)
{
struct stat statbuf;
if(lstat(path, &statbuf) ==0)//lstat返回文件的信息,文件信息存放在stat结构中
{
return S_ISDIR(statbuf.st_mode) != 0;//S_ISDIR宏,判断文件类型是否为目录
}
return false;
}
//判断是否为常规文件
bool is_file(const char *path)
{
struct stat statbuf;
if(lstat(path, &statbuf) ==0)
return S_ISREG(statbuf.st_mode) != 0;//判断文件是否为常规文件
return false;
}
//判断是否是特殊目录
bool is_special_dir(const char *path)
{
return strcmp(path, ".") == 0 || strcmp(path, "..") == 0;
}
//生成完整的文件路径
void get_file_path(const char *path, const char *file_name, char *file_path)
{
strcpy(file_path, path);
if(file_path[strlen(path) - 1] != '/')
strcat(file_path, "/");
strcat(file_path, file_name);
}
void delete_file(const char *path)
{
DIR *dir;
dirent *dir_info;
char file_path[PATH_MAX];
if(is_file(path))
{
remove(path);
return;
}
if(is_dir(path))
{
if((dir = opendir(path)) == NULL)
return;
while((dir_info = readdir(dir)) != NULL)
{
get_file_path(path, dir_info->d_name, file_path);
if(is_special_dir(dir_info->d_name))
continue;
delete_file(file_path);
rmdir(file_path);
}
}
}
//拷贝源文件夹中的文件到目标文件夹
int cpfile(char const* from,char const* to)//此处的路径具体到文件名
{
FILE* fd_from=fopen(from,"r");
printf("sourcefile:%s\n",from);
if(fd_from==NULL)
{
printf("open %s failed!!!\n",from);
return -1;
}
FILE* fd_to=fopen(to,"w");
if(fd_to==NULL)
{
printf("open %s failed!!!\n",to);
return -1;
}
char buffer[100000];
int read_size=100000;
int write_size=100000;
while(read_size==100000)
{
read_size=fread(buffer,1,100000,fd_from);
write_size=fwrite(buffer,1,read_size,fd_to);
if(read_size!=write_size)
{
printf("write file error!!!\n");
}
}
fclose(fd_from);
fclose(fd_to);
return 0;
}
int goindir(const char *dir,string* ret_sons)//进入文件夹内部
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open directory: %s\n", dir);
return -1;
}
int files_cnt=0;
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf);
//if(S_ISDIR(statbuf.st_mode)) {
printf(entry->d_name);
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
ret_sons[files_cnt]=entry->d_name;
files_cnt++;
//}
}
closedir(dp);
return files_cnt;
}
int find_in_otherdir(const char * from_father,const char* to_path)//判断文件是否在其他路径中存在
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(from_father)) == NULL)
{
fprintf(stderr,"cannot open directory: %s\n", from_father);
return -1;
}
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
string temp=entry->d_name;
if(temp==to_path)
{
printf("%s is included in %s!!!\n",to_path,from_father);
return 0;
}
}
}
closedir(dp);
return 1;
}
bool is_dir(const char *path)
{
struct stat statbuf;
if(lstat(path, &statbuf) ==0)//lstat返回文件的信息,文件信息存放在stat结构中
{
return S_ISDIR(statbuf.st_mode) != 0;//S_ISDIR宏,判断文件类型是否为目录
}
return false;
}
//判断是否为常规文件
bool is_file(const char *path)
{
struct stat statbuf;
if(lstat(path, &statbuf) ==0)
return S_ISREG(statbuf.st_mode) != 0;//判断文件是否为常规文件
return false;
}
//判断是否是特殊目录
bool is_special_dir(const char *path)
{
return strcmp(path, ".") == 0 || strcmp(path, "..") == 0;
}
//生成完整的文件路径
void get_file_path(const char *path, const char *file_name, char *file_path)
{
strcpy(file_path, path);
if(file_path[strlen(path) - 1] != '/')
strcat(file_path, "/");
strcat(file_path, file_name);
}
void delete_file(const char *path)
{
DIR *dir;
dirent *dir_info;
char file_path[PATH_MAX];
if(is_file(path))
{
remove(path);
return;
}
if(is_dir(path))
{
if((dir = opendir(path)) == NULL)
return;
while((dir_info = readdir(dir)) != NULL)
{
get_file_path(path, dir_info->d_name, file_path);
if(is_special_dir(dir_info->d_name))
continue;
delete_file(file_path);
rmdir(file_path);
}
}
}
//拷贝源文件夹中的文件到目标文件夹
int cpfile(char const* from,char const* to)//此处的路径具体到文件名
{
FILE* fd_from=fopen(from,"r");
printf("sourcefile:%s\n",from);
if(fd_from==NULL)
{
printf("open %s failed!!!\n",from);
return -1;
}
FILE* fd_to=fopen(to,"w");
if(fd_to==NULL)
{
printf("open %s failed!!!\n",to);
return -1;
}
char buffer[100000];
int read_size=100000;
int write_size=100000;
while(read_size==100000)
{
read_size=fread(buffer,1,100000,fd_from);
write_size=fwrite(buffer,1,read_size,fd_to);
if(read_size!=write_size)
{
printf("write file error!!!\n");
}
}
fclose(fd_from);
fclose(fd_to);
return 0;
}
int goindir(const char *dir,string* ret_sons)//进入文件夹内部
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open directory: %s\n", dir);
return -1;
}
int files_cnt=0;
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf);
//if(S_ISDIR(statbuf.st_mode)) {
printf(entry->d_name);
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
ret_sons[files_cnt]=entry->d_name;
files_cnt++;
//}
}
closedir(dp);
return files_cnt;
}
int find_in_otherdir(const char * from_father,const char* to_path)//判断文件是否在其他路径中存在
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(from_father)) == NULL)
{
fprintf(stderr,"cannot open directory: %s\n", from_father);
return -1;
}
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
string temp=entry->d_name;
if(temp==to_path)
{
printf("%s is included in %s!!!\n",to_path,from_father);
return 0;
}
}
}
closedir(dp);
return 1;
}
- Linux 文件管理
- linux文件管理
- linux 文件 Cache 管理
- linux 文件 Cache 管理
- linux 文件 Cache 管理
- Linux文件管理常用命令
- Linux 文件管理
- Linux文件管理常用命令
- Linux文件/文件夹管理
- Linux 文件管理常用命令
- linux文件权限管理
- Linux文件/文件夹管理
- Linux设备文件管理
- 【linux基础】文件管理
- linux文件管理
- Linux文件管理
- linux 文件 Cache 管理
- Linux文件目录管理
- ARM汇编指令集
- Qt 小记载
- win32窗口程序开发步骤详解
- stm32+ucosii hardhandle调试经历
- 远程连接SQL Server 2008,服务器端和客户端配置
- linux文件管理
- Program Stream 解析代码
- DE2-115网口延时问题
- 数据结构与算法 python实现
- IOS-项目总结
- DM36x IPNC OSD显示中文 --- 基本数据准备篇
- Win32程序的开发流程和运行图解
- springMVC之annotation优化
- 来这里做一下技术笔记