文件操作,读文件、写文件、获取文件长度、删除文件、判断文件格式等。

这个代码集合包含了一系列用于文件操作的函数,如获取文件大小、复制文件、追加写入文件、读取文件、删除文件、创建多级目录、检查文件和目录是否存在以及获取文件后缀名。此外,还提供了检查文件是否为jpg格式的功能。
摘要由CSDN通过智能技术生成
#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值