标签:
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include<sys/types.h>
#include<fcntl.h>
#include <assert.h>
//判断是否为目录
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_dirs_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_dirs_file(file_path);
rmdir(file_path);
}
}
}
//判断文件是否存在
bool is_file_exist(const char *dir) {
if (dir == NULL)
return false;
if ((access(dir,F_OK))!=-1){
printf(" 文件存在\n");
return true;
}
printf(" 文件不存在\n");
return false;
}
//判断文件目录是否存在
bool is_dir_exist(const char *dir) {
if (dir == NULL)
return false;
if (opendir(dir) == NULL)
{
printf(" 文件目录不存在\n");
return false;
}
printf(" 文件目录存在\n");
return true;
}
//创建多级目录
bool create_dirs(const char *dir) {
if (dir == NULL)
return false;
char DirName[256];
strcpy(DirName, dir);
int i, len = strlen(DirName);
if (DirName[len - 1] != '/')
strcat(DirName, "/");
len = strlen(DirName);
for (i = 1; i < len; i++) {
if (DirName[i] == '/') {
DirName[i] = 0;
//判断路径是否存在,如果不存在则创建路径
if (!is_dir_exist(DirName) ) {
if (mkdir(DirName, 0755) == -1) {
perror("mkdir error");
return false;
}
}
DirName[i] = '/';
}
}
return true;
}
//创建文件可读写的文件
bool create_file(const char *file_path, const char *file_name) {
if(is_dir_exist(file_path)){
//文件已经存在
if (is_file_exist(file_name)) {
return true;
}
}
else{
//文件目录不存在,创建目录
if (create_dirs(file_path)) {
// 创建目录成功,这里去创建文件
if(open(file_name,O_RDWR | O_CREAT,
S_IRUSR | S_IRGRP | S_IROTH)!=-1)
return true;
}
}
return false;
}
//删除文件
bool delete_file(const char* file_path) {
if (file_path == NULL) {
return false;
}
if(remove(file_path)==-1)
return false;
return true;
}
//以追加的方式写入文件
bool write_file(const char *fileName,const char *content) {
FILE *fp;
fp = fopen(fileName, "a+");
if (NULL == fp) {
return false;
}
if( fwrite(content, strlen(content), 1, fp)!=1)
return false;
fclose (fp);
return true;
}
int main(int argc, char **argv)
{
// create_dirs("/Users/sunger/desktop/sunger/a/b/c");
is_file_exist("/Users/sunger/desktop/a.c");
read_file("/Users/sunger/desktop/a.c");
return 0;
}
在mac下测试全部通过标签:
原文地址:http://blog.csdn.net/s228245214/article/details/45182085