C语言关于文件处理函数

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; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值