帮同事写的一个C

本文介绍了一个基于C语言实现的文件管理系统,该系统能够根据配置文件中设定的时间间隔自动删除指定文件。通过读取配置文件获取删除时间间隔,并利用系统时间进行比较,达到时间条件后删除文件并记录日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* Note:Your choice is C IDE */
#include <stdio.h>
#include <dir.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <io.h>
#include <time.h>
/*#include <sys/stat.h>*/
struct RMfileName{
 char *filename;
 struct RMfileName *pNext;
};
struct RMFileList
{
 struct RMfileName *pfisrt;
 struct RMfileName *plast;
};
struct RMFileList pFlist;
struct tm rmtime[3];
char *GetInitKey(char *filename, char *title, char *key);
int charToInt(char* f);

int  readtime(char *filename,struct tm *time);
int  readfile(char *filename);

int  insertList(struct RMfileName *file,struct RMFileList *p);
int  deleteItem(struct RMfileName *temp);
int  deleteAllList(struct RMFileList p);
int  deletefile(char *file);
int  abjusttime(struct tm *time);
int  compaytime(struct tm time,struct tm time1);
int main()
{
 /*system */
    int mkdirfile = 0;
    struct tm *timeinfo;
    FILE *unrmfilefp;
    FILE *rmfilefp;
    time_t rawtime;
    char *ptr = NULL  ;
    struct RMfileName *RMtemp;
    int flag = 0;
    char m = ',';
    pFlist.pfisrt = NULL;
    pFlist.plast = NULL;

    clrscr();
 printf("start Run...../n");

 time(&rawtime);
 timeinfo = localtime ( &rawtime );
 /*log file is create*/
 unrmfilefp = fopen("unrmfilefp.txt","wa");
 rmfilefp = fopen("rmfilefp.txt","wa");
 
 if(unrmfilefp && rmfilefp){
 printf("log file is create/n");
 }
 /*current time*/
 memset(&rmtime[2],0,sizeof(struct tm));
 ptr = memcpy(&rmtime[0], timeinfo, sizeof(struct tm));
 if(NULL == ptr){
     printf("memcpy(&rmtime[1],timeinfo,strlen(timeinfo) is fail!/n");
     return ;
 }
 while(1){
  /* get spacing time */
     while(1){
    time(&rawtime);
             timeinfo = localtime(&rawtime);
             if(compaytime(rmtime[0],*timeinfo) > 0){
                sleep(10);
              }
             if(compaytime(rmtime[0],*timeinfo) <= 0){
              deleteAllList(pFlist);
              readfile("config.ini");
              RMtemp = pFlist.pfisrt;
                 while(RMtemp){
                      if(!deletefile(RMtemp->filename)){
        RMtemp =  pFlist.pfisrt;
        pFlist.pfisrt = pFlist.pfisrt->pNext;
        fseek(rmfilefp,0,SEEK_END);
        fwrite(RMtemp->filename,strlen(RMtemp->filename),1,rmfilefp);
        fwrite(&m,1,1,rmfilefp);

        printf(RMtemp->filename);
        printf(" is deleted");
        printf(",the number of file that is deleted is  %d/n",flag);
        printf("%d--%d--%d  %d:%d:%d/n",timeinfo->tm_year + 1900,timeinfo->tm_mon,
        timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
        deleteItem(RMtemp);
        RMtemp = pFlist.pfisrt ;
        flag ++;
        }
                      else{
                       if(RMtemp){
                       fseek(unrmfilefp,0,SEEK_END);
        fwrite(RMtemp->filename,strlen(RMtemp->filename),1,unrmfilefp);
        fwrite(&m,1,1,unrmfilefp);
        printf("%d--%d--%d  %d:%d:%d/n",timeinfo->tm_year + 1900,timeinfo->tm_mon,
        timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
                       printf(RMtemp->filename);
        printf(" do not delete/n");
                          RMtemp = RMtemp->pNext;
                       }
                       else {
                        break;
                       }
                      }
                   }
                 break;
              }

     }
     readtime("config.ini",&rmtime[1]);
  rmtime[0].tm_year +=rmtime[1].tm_year;
  rmtime[0].tm_mon +=rmtime[1].tm_year;
  rmtime[0].tm_mday +=rmtime[1].tm_mday;
  rmtime[0].tm_hour +=rmtime[1].tm_hour;
  rmtime[0].tm_min +=rmtime[1].tm_min;
  rmtime[0].tm_sec +=rmtime[1].tm_sec;
  /*  abjust time */
     abjusttime(&rmtime[0]);
     
     rmtime[2].tm_year +=rmtime[1].tm_year;
  rmtime[2].tm_mon +=rmtime[1].tm_year;
  rmtime[2].tm_mday +=rmtime[1].tm_mday;
  rmtime[2].tm_hour +=rmtime[1].tm_hour;
  rmtime[2].tm_min +=rmtime[1].tm_min;
  rmtime[2].tm_sec +=rmtime[1].tm_sec;
  abjusttime(&rmtime[0]);
  printf("RUN TIME ");
  printf("%d--%d--%d  %d:%d:%d/n",rmtime[2].tm_year,rmtime[2].tm_mon,
  rmtime[2].tm_mday,rmtime[2].tm_hour,rmtime[2].tm_min,rmtime[2].tm_sec);
     }
 fclose(unrmfilefp);
 fclose(rmfilefp);       
 deleteAllList(pFlist);
 return 0;
}
int readtime(char *filename,struct tm *time)
{
 int handle ;
 char *t;

    t = GetInitKey(filename, "spacingtime", "year");
    time->tm_year = charToInt(t);
    t = GetInitKey(filename, "spacingtime", "month");
    time->tm_mon = charToInt(t);
    t = GetInitKey(filename, "spacingtime", "day");
    time->tm_mday = charToInt(t);
    t = GetInitKey(filename, "spacingtime", "hour");
    time->tm_hour = charToInt(t);
    t = GetInitKey(filename, "spacingtime", "minute");
    time->tm_min = charToInt(t);
    t = GetInitKey(filename, "spacingtime", "second") ;
    time->tm_sec = charToInt(t);

 return 0;
}
int deletefile(char *file)
{
 int i=0;
 int FA_RDONLY = 0 ;
 if((access(file, 0)) == -1){
  return 0;
 }
 _chmod(file,1,&FA_RDONLY);
 i = remove(file);
 return i;

}
int abjusttime(struct tm * time)
{
 while(time->tm_sec >= 60 ||time->tm_min >= 60 ||time->tm_hour > 24)
 {
  if(time->tm_sec >= 60) {
   time->tm_min += 1;
   time->tm_sec  = time->tm_sec - 60;
  }
  if(time->tm_min >= 60) {
   time->tm_hour += 1;
   time->tm_min  = time->tm_min - 60;
  }
  if(time->tm_hour > 24) {
   time->tm_mday +=  1;
   time->tm_hour  = time->tm_hour - 24;
  }
 }
 while(1)
 {
  if ((time->tm_mon == 1||time->tm_mon == 3||time->tm_mon == 5||
   time->tm_mon == 7||time->tm_mon == 8||time->tm_mon == 10||
   time->tm_mon == 12)){
    if(time->tm_mday > 31){
              time->tm_mday = time->tm_mday - 31;
              time->tm_mon ++;
              }
  }
  if ((time->tm_mon == 4 || time->tm_mon == 6 || time->tm_mon == 9 ||
   time->tm_mon == 11)){
    if(time->tm_mday > 30){
              time->tm_mday = time->tm_mday - 30;
              time->tm_mon ++;
              }
  }
  if (time->tm_mon == 2){
    if(!(time->tm_year%4) && (time->tm_mday > 29)){
               time->tm_mday = time->tm_mday - 29;
               time->tm_mon ++;
    }
   if((time->tm_year%4) && time->tm_mday > 28){
              time->tm_mday = time->tm_mday - 28;
              time->tm_mon ++;
   }
  }
  if((time->tm_mday) <= 28){
   break;
  }
  if(time->tm_mday == 29 && time->tm_mon != 2){
   break;
  }
  if(time->tm_mday == 29 && time->tm_mon == 2 && !(time->tm_year%4)){
   break;
  }
  if(time->tm_mday == 30 && time->tm_mon != 2){
   break;
  }
  if(time->tm_mday == 31 && time->tm_mon != 2 && time->tm_mon != 4 &&
   time->tm_mon != 6 && time->tm_mon != 9 && time->tm_mon != 11){
   break;
  }

 }
 while(1){
  if (time->tm_mon > 12){
   time->tm_year ++;
   time->tm_mon = time->tm_mon - 12;
  }
  else{
   break;
   }
  }
 return 0;
}
int readfile(char *filename)
{
 char *f = NULL;
 int  flag = 0;
 int count=0;
 struct RMfileName *file;
 /* open file*/
 char *tempname = (char*)malloc(128);
 f = GetInitKey(filename,"rmfile","name");
 while(f&&*f != '/0')
 {
  count = 0;
  while(*f != ',')
  {
   if(*f =='/n'||*f ==' '){
     f++;
     }
   if(*f =='/0'){
     flag = 1;
     break;
     }
   *(tempname +count) = *f;
   f ++; 
   count ++;
  }
  *(tempname +count) = '/0';
  file=(struct RMfileName *)malloc(sizeof(struct RMfileName));
  count = strlen(tempname);
  file->filename = (char *)malloc(count + 1);
  strcpy(file->filename,tempname);
  file->filename[count + 2]='/0';
  file->pNext = NULL;
  if(file->filename){
   insertList(file,&pFlist);
  }
  if(flag == 1) break;
  else {
   f ++;
      }
 }
 return 0;
}
int  insertList(struct RMfileName *file,struct RMFileList *p)
{
 struct RMfileName *temp;
 if(file == NULL) return 0;
 else{
  if(p->pfisrt == NULL){
   p->pfisrt = file;
   p->plast = file;
  }
  else{
   p->plast->pNext = file;
   p->plast = file;
  }
 }
 return 0;
}
int  deleteAllList(struct RMFileList p)
{
 struct RMfileName *temp;
 while(1){
  if(p.pfisrt == NULL){
     break;
  }
  else{
   temp = p.pfisrt;
   free((char*)temp->filename);
   free((struct RMfileName *)temp);
   temp =NULL;
   p.pfisrt = temp->pNext;
   }
 }
}
int charToInt(char* f)
{
 int i =1;
 int data = 0;
 if(f == NULL) return 1;
 else{
  while(1)
  {
   if((!f))break;
   if((*f =='/0')) break;
   if((*f < '0'))break;
   if((*f > '9'))break;

   data = data * i;
   data += (*f - '0');
   f ++;
   i *= i * 10;
  }
  return data;
 }
}
char *GetInitKey(char *filename, char *title, char *key)
{
    FILE *fp;
    char tmpLine[1024];
    int rtnval;
    int i = 0;
    int flag = 0;
    char *tmp;
    static char tmpstr[1024];


    if ((fp = fopen(filename, "r")) == NULL) {
      return "have no such file";
    }
    while (!feof(fp)) {
    rtnval = fgetc(fp);
    if (rtnval == EOF) {
        tmpLine[i++] = 0;
    } else {
        tmpLine[i++] = rtnval;
    }
    if (rtnval == '/n' || rtnval == EOF) {
        tmpLine[--i] = 0;
        i = 0;

        if (tmpLine[0] == '/0' || strchr("#;", tmpLine[0]) != NULL)
        continue;  
        if (tmpLine[0] == '[') {
        tmp = NULL;
        if (flag == 1) {
          
#ifdef PROCESS_DUP_SECTION   
            flag = 0;
#else
            break;  
#endif
        }
        } else {
        tmp = strchr(tmpLine, '=');
        }

        if ((tmp != NULL) && (flag == 1)) {
        char *p;

        *tmp = '/0';

        p = strstr(tmpLine, key);
        if (p != NULL) {
            if (p > tmpLine && strchr(" /t", *(p - 1)) == NULL)
            continue;

            p += strlen(key);
            if (*p == '/0' || strchr(" /t", *p) != NULL) {
            tmp++;
            while (*tmp == ' ' || *tmp == '/t')
                tmp++;
            strcpy(tmpstr, tmp);
            fclose(fp);
            return tmpstr;
            }
        }
        } else {
        strcpy(tmpstr, "[");
        strcat(tmpstr, title);
 strcat(tmpstr, "]");
 if (strcmp(tmpstr, tmpLine) == 0) {
            flag = 1;
        }
        }

    }

    if (rtnval == EOF) {
        break;
    }
    }
    fclose(fp);
    return "";
}
int  compaytime(struct tm time,struct tm time1)
{
 int i = 0;
 if(time.tm_year > time1.tm_year){
  i = 1;
  return i;
 }
 if(time.tm_year < time1.tm_year){
  i = -1;
  return i;
 }
 
 if(time.tm_mon > time1.tm_mon){
  i = 1;
  return i;
 }
 if(time.tm_mon < time1.tm_mon){
  i = -1;
  return i;
 }
 
 if(time.tm_mday > time1.tm_mday){
  i = 1;
  return i;
  }
 if(time.tm_mday > time1.tm_mday){
  i = -1;
  return i;
 }
 
 if(time.tm_hour > time1.tm_hour){
  i = 1;
  return i;
  }
 if(time.tm_hour < time1.tm_hour){
  i = -1;
  return i;
  }
 
 if(time.tm_min > time1.tm_min){
  i = 1;
  return i;
  }
 if(time.tm_min < time1.tm_min){
  i = -1;
  return i;
  }
 return i;
}
int deleteItem(struct RMfileName *temp)
{
 free(temp->filename);
 free(temp);
    return 0; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值