C语言关于文件处理函数(包括获取文件大小、判断文件是否存在、删除、获取文件数目、文件排序、剩余空间等)
代码:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <unistd.h>
typedef unsigned int uint32;
typedef int int32;
typedef unsigned char uint8;
typedef char int8;
//得到文件大小
int32 get_filesize(int8 *path)
{
struct stat buf;
int32 ret;
if(NULL == path)
return -1;
ret = stat(path,&buf);
if(ret == -1)
return -1;
return buf.st_size;
}
//文件是否存在
int32 is_fileexist(int8 *path)
{
struct stat buf;
int32 ret;
if(NULL == path)
return FALSE;
ret = stat(path,&buf);
if(-1 == ret)
{
return FALSE;
}
return TRUE;
}
//删除指定路径文件或目录
int32 del_file(int8 *path)
{
int8 com_str[255]; //命令字符串
DIR * dir;
struct dirent * ptr;
uint8 filepath[100];
int32 ret;
if(NULL == path)
{
return FALSE;
}
if(is_fileexist(path) == FALSE)
{
return TRUE;
}
if(path[strlen(path) - 1] == '/')
{
dir = opendir(path);
while((ptr = readdir(dir))!=NULL)
{
if((strcmp(ptr->d_name,".") != 0) && (strcmp(ptr->d_name,"..") != 0))
{
sprintf(filepath,"%s%s",path,ptr->d_name);
printf("del_file filepath:%s/n",filepath);
remove(filepath);
sprintf(filepath,"%s/",filepath);
printf("In del_file filepath:%s/n",filepath);
del_file(filepath);
sync();
}
}
}
else
{ printf("del file %s/n",path); //cx
ret = remove(path);
sync();
printf("sync()/n");
printf("ret %d/n",ret); //cx
if(0 != ret)
{
return FALSE;
}
return TRUE;
}
}
//获得指定路径下的文件数
int32 get_filenum(int8 *path)
{
DIR *dir;
struct dirent *ptr;
int num = 0;
dir = opendir(path);
if(NULL == dir){
perror("opendir:");
return 0;
}
while((ptr = readdir(dir)) != NULL)
{
if((strcmp(ptr->d_name,".") != 0) && (strcmp(ptr->d_name,"..") != 0))
{
//if(check_file(ptr->d_name) )
num++;
}
}
closedir(dir);
return num;
}
//按字母升序或降序排列文件名
void ls_sort(uint8 *filepath,int32 sort)
{
struct dirent **namelist;
int n,i,j = 1;
char buf[50];
n = scandir(filepath, &namelist, 0, alphasort);
if (n < 0)
perror("scandir:");
else{
if(sort == 0)
while(n--){
if(strcmp(namelist[n]->d_name,".") != 0 && strcmp(namelist[n]->d_name,"..") != 0){
if(check_file(namelist[n]->d_name) == 0)
continue;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/n",namelist[n]->d_name);
printf("%d: %s",j,buf);
j++;
}
free(namelist[n]);
}
else
for(i = 0;i < n;i++){
if(strcmp(namelist[i]->d_name,".") != 0 && strcmp(namelist[i]->d_name,"..") != 0){
if(check_file(namelist[i]->d_name) == 0)
continue;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/n",namelist[i]->d_name);
printf("%d: %s",j,buf);
j++;
}
free(namelist[i]);
}
free(namelist);
}
}
//得到某一路径下剩余空间等的一些信息
void c_files(int8 *path)
{
struct statfs buf;
int8 ret = statfs(path,&buf);
printf("%ld %ld %ld/n",buf.f_bfree,buf.f_type,buf.f_blocks);
}
//读取指定的文件中一行到一字符串中
int readline(FILE *f,char *s)
{
char c;
do{
c=fgetc(f);
*(s++)=c;
}while(c!=EOF&&c!='/xa'&&c!='/xd');
*(s-1)='/0';
return c;
}
代码:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <unistd.h>
typedef unsigned int uint32;
typedef int int32;
typedef unsigned char uint8;
typedef char int8;
//得到文件大小
int32 get_filesize(int8 *path)
{
struct stat buf;
int32 ret;
if(NULL == path)
return -1;
ret = stat(path,&buf);
if(ret == -1)
return -1;
return buf.st_size;
}
//文件是否存在
int32 is_fileexist(int8 *path)
{
struct stat buf;
int32 ret;
if(NULL == path)
return FALSE;
ret = stat(path,&buf);
if(-1 == ret)
{
return FALSE;
}
return TRUE;
}
//删除指定路径文件或目录
int32 del_file(int8 *path)
{
int8 com_str[255]; //命令字符串
DIR * dir;
struct dirent * ptr;
uint8 filepath[100];
int32 ret;
if(NULL == path)
{
return FALSE;
}
if(is_fileexist(path) == FALSE)
{
return TRUE;
}
if(path[strlen(path) - 1] == '/')
{
dir = opendir(path);
while((ptr = readdir(dir))!=NULL)
{
if((strcmp(ptr->d_name,".") != 0) && (strcmp(ptr->d_name,"..") != 0))
{
sprintf(filepath,"%s%s",path,ptr->d_name);
printf("del_file filepath:%s/n",filepath);
remove(filepath);
sprintf(filepath,"%s/",filepath);
printf("In del_file filepath:%s/n",filepath);
del_file(filepath);
sync();
}
}
}
else
{ printf("del file %s/n",path); //cx
ret = remove(path);
sync();
printf("sync()/n");
printf("ret %d/n",ret); //cx
if(0 != ret)
{
return FALSE;
}
return TRUE;
}
}
//获得指定路径下的文件数
int32 get_filenum(int8 *path)
{
DIR *dir;
struct dirent *ptr;
int num = 0;
dir = opendir(path);
if(NULL == dir){
perror("opendir:");
return 0;
}
while((ptr = readdir(dir)) != NULL)
{
if((strcmp(ptr->d_name,".") != 0) && (strcmp(ptr->d_name,"..") != 0))
{
//if(check_file(ptr->d_name) )
num++;
}
}
closedir(dir);
return num;
}
//按字母升序或降序排列文件名
void ls_sort(uint8 *filepath,int32 sort)
{
struct dirent **namelist;
int n,i,j = 1;
char buf[50];
n = scandir(filepath, &namelist, 0, alphasort);
if (n < 0)
perror("scandir:");
else{
if(sort == 0)
while(n--){
if(strcmp(namelist[n]->d_name,".") != 0 && strcmp(namelist[n]->d_name,"..") != 0){
if(check_file(namelist[n]->d_name) == 0)
continue;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/n",namelist[n]->d_name);
printf("%d: %s",j,buf);
j++;
}
free(namelist[n]);
}
else
for(i = 0;i < n;i++){
if(strcmp(namelist[i]->d_name,".") != 0 && strcmp(namelist[i]->d_name,"..") != 0){
if(check_file(namelist[i]->d_name) == 0)
continue;
memset(buf,0,sizeof(buf));
sprintf(buf,"%s/n",namelist[i]->d_name);
printf("%d: %s",j,buf);
j++;
}
free(namelist[i]);
}
free(namelist);
}
}
//得到某一路径下剩余空间等的一些信息
void c_files(int8 *path)
{
struct statfs buf;
int8 ret = statfs(path,&buf);
printf("%ld %ld %ld/n",buf.f_bfree,buf.f_type,buf.f_blocks);
}
//读取指定的文件中一行到一字符串中
int readline(FILE *f,char *s)
{
char c;
do{
c=fgetc(f);
*(s++)=c;
}while(c!=EOF&&c!='/xa'&&c!='/xd');
*(s-1)='/0';
return c;
}