#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/stat.h>
#define FILE_COPY_SIZE (16 * 1024)
获取文件长度
unsigned long get_file_size(const char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) < 0)
return -1;
else
return statbuf.st_size;
}
复制文件
int file_copy(char *src_file_path, char *dest_file_path)
{
FILE *dest_fp = NULL, *src_fp = NULL;
char *buff = NULL;
int read_cnt, write_cnt, ret = -1;
buff = malloc(FILE_COPY_SIZE);
if (!buff) {
printf("%s: alloc file buffer failed", __func__);
goto free_res;
}
dest_fp = fopen(dest_file_path, "wb");
if (dest_fp == NULL) {
printf("%s: open %s failed\n", __func__, dest_file_path);
goto free_res;
}
src_fp = fopen(src_file_path, "r");
if (src_fp == NULL) {
printf("%s: %s not found\n", __func__, src_file_path);
goto free_res;
}
while (1) {
read_cnt = fread(buff, 1, FILE_COPY_SIZE, src_fp);
if (read_cnt > 0) {
write_cnt = fwrite(buff, 1, read_cnt, dest_fp);
if (read_cnt != write_cnt) {
printf("%s: write file failed\n", __func__);
goto free_res;
}
} else
break;
}
ret = 0;
free_res:
if (buff)
free(buff);
if (dest_fp) {
fflush(dest_fp);
fclose(dest_fp);
}
if (src_fp)
fclose(src_fp);
return ret;
}
写文件
int file_write_append(char *file_path, char *buf, int size)
{
FILE *flip = NULL;
int count = 0;
flip = fopen(file_path, "ab+");
if (flip == NULL) {
printf("%s:open %s failed\n", __func__, file_path);
return -1;
}
count = fwrite(buf, 1, size, flip);
if (count != size) {
printf("write file(%s) failed, size:%d != write_count:%d\n",
file_path, size, count);
fflush(flip);
fclose(flip);
return -1;
}
fflush(flip);
fclose(flip);
return 0;
}
读文件
int file_read(char *file_path, char *buff, int length)
{
FILE *flip = NULL;
int count = 0;
flip = fopen(file_path, "rb");
if (flip == NULL) {
printf("%s:open %s failed\n", __func__, file_path);
return -1;
}
count = fread(buff, 1, length, flip);
if (count != length) {
printf("%s:read %s failed \n", __func__, file_path);
fclose(flip);
return -1;
}
fclose(flip);
return 0;
}
写文件
int file_write(char *file_path, char *buf, int size)
{
FILE *flip = NULL;
int count = 0;
flip = fopen(file_path, "wb");
if (flip == NULL) {
printf("%s:open %s failed\n", __func__, file_path);
return -1;
}
count = fwrite(buf, 1, size, flip);
if (count != size) {
printf("write file(%s) failed, size:%d != write_count:%d\n",
file_path, size, count);
fflush(flip);
fclose(flip);
return -1;
}
fflush(flip);
fclose(flip);
return 0;
}
获取文件长度
int file_length(char *file_path)
{
FILE *fp = NULL;
int file_len;
fp = fopen(file_path, "rb");
if (fp == NULL) {
printf("open %s failed", file_path);
return -1;
}
fseek(fp, 0, SEEK_END);
file_len = ftell(fp);
if (file_len < 0) {
printf("get file length error");
fclose(fp);
return -1;
}
fseek(fp, 0, SEEK_SET);
fclose(fp);
return file_len;
}
删除文件
int file_move(char *src_file_path, char *dest_file_path)
{
int ret;
ret = file_copy(src_file_path, dest_file_path);
if (ret) {
printf("failed to copy %s to %s", src_file_path, dest_file_path);
return ret;
}
ret = remove(src_file_path);
if (ret) {
printf("failed to remove %s", src_file_path);
remove(dest_file_path);
return ret;
}
return 0;
}
创建目录
int mk_mul_dirs(char *muldir)
{
int i, len;
char str[128];
int ret = 0;
strcpy(str, muldir);
len = strlen(str);
for (i = 0; i < len; i++) {
if ((str[i] == '/') && i) {
str[i] = '\0';
if (access(str, 0) != 0) {
ret = mkdir(str, 0777);
if (ret != 0) {
printf("Create multiple folder: %s fail\n", str);
return -1;
}
}
str[i] = '/';
}
}
if (len > 0 && access(str, 0) != 0) {
ret = mkdir(str, 0777);
if (ret != 0) {
printf("Create multiple folder: %s fail\n", str);
return -1;
}
}
return 0;
}
判断文件是否存在
int is_file_exist(char *file_path)
{
if (!file_path)
return -1;
if (access(file_path, F_OK) != -1)
return 0;
return -1;
}
判断目录是否存在
int is_dir_exist(const char *dir_path)
{
DIR *dir;
if (!dir_path)
return -1;
dir = opendir(dir_path);
if (!dir)
return -1;
closedir(dir);
return 0;
}
获取文件后缀名
int get_filename_ext(const char *filename, char *ext, int ext_max_len)
{
int ret = 0;
const char *dot = NULL;
assert((filename != NULL) && (ext != NULL));
dot = strrchr(filename, '.');
if (!dot || (dot == filename))
ret = -1;
else {
ret = 0;
snprintf(ext, ext_max_len, "%s", dot + 1);
}
return ret;
}
判断文件是否是jpg文件
int check_file_is_jpg(char *file_name)
{
int ret = -1;
int cmp_ret = 0;
char ext[64];
char jpg_ext[] = "jpg";
assert(file_name != NULL);
memset(ext, 0, sizeof(ext));
ret = get_filename_ext(file_name, ext, sizeof(ext));
if (0 == ret) {
cmp_ret = strcasecmp(ext, jpg_ext);
if (0 == cmp_ret)
ret = 0;
else
ret = -1;
}
return ret;
}