用以删除一个目录下类似“# *****”格式的配置文件注释,只可删除单独的行。

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <dirent.h>   
  4. #include <time.h>   
  5.    
  6. int traversal_dir(const char *dir, int dept);   
  7. int del_file_com(const char *file_path);   
  8. int main(int argc, char *argv[])   
  9. {   
  10.     if(argc<2) {   
  11.         fprintf(stdout, "input dir...\n");   
  12.         return -1;   
  13.     }   
  14.     int dept = 0;   
  15.     traversal_dir(argv[1], dept);   
  16.     return 0;   
  17. }   
  18. int traversal_dir(const char *dir, int dept)   
  19. {   
  20.     DIR *dirptr = NULL;   
  21.     if(NULL == (dirptr=opendir(dir))) {   
  22.         fprintf(stdout, "no such dirpath:%s\n", dir);   
  23.         return -1;   
  24.     }   
  25.     struct dirent *entry;   
  26.     while(NULL != (entry=readdir(dirptr))) {   
  27.         //when the entry is sub_dir into it   
  28.         if((!strcmp(entry->d_name, ".")) || (!strcmp(entry->d_name, ".."))) {   
  29.             continue;   
  30.         }   
  31.         int i;   
  32.         for(i=0; i<dept; ++i) {   
  33.             fprintf(stdout, " ");   
  34.         }   
  35.         char dir_buf[1024];   
  36.         snprintf(dir_buf, sizeof(dir_buf), "%s/%s", dir, entry->d_name);   
  37.         if(DT_DIR == entry->d_type) { /* 4 */   
  38.             fprintf(stdout, "dir:%s/%s\n", dir, entry->d_name);   
  39.             traversal_dir(dir_buf, dept+4);   
  40.         } else if (DT_REG == entry->d_type) {   
  41.             fprintf(stdout, "file:%s\n", entry->d_name);   
  42.             del_file_com(dir_buf);   
  43.         } else {   
  44.             continue;   
  45.         }   
  46.     }   
  47.     return 0;   
  48. }   
  49. int del_file_com(const char *file_path)   
  50. {   
  51.     const char *old_file = file_path;   
  52.     char new_file[1024];   
  53.     snprintf(new_file, sizeof(new_file), "%s.%ld", old_file, time(NULL));   
  54. //  fprintf(stdout, "del_file_com...\n");   
  55. //  fprintf(stdout, "old_file:%s, new_file:%s\n", old_file, new_file);   
  56.     FILE *fp_from = fopen(old_file, "r");   
  57.     FILE *fp_to = fopen(new_file, "w");   
  58.     if((NULL==fp_from) || (NULL==fp_to)) {   
  59.         return -1;   
  60.     }   
  61.     char line[1024];   
  62.     char *cp = line;   
  63.     while(NULL != (fgets(cp, sizeof(line), fp_from))) {   
  64.         while(((' '==*cp)||('\t'==*cp)||('\n'==*cp)) && ('\0'!=*cp)) {   
  65.             ++cp;   
  66.         }   
  67.         if(('#'!=*cp) && ('\0'!=*cp)) {   
  68.             fprintf(fp_to, "%s", line);   
  69.         }   
  70.         cp = line;   
  71.     }   
  72.     fclose(fp_from);   
  73.     fclose(fp_to);   
  74.     rename(new_file, old_file);   
  75.     return 0;   
  76. }   

备注:

  1. struct dirent   
  2.   {   
  3. #ifndef __USE_FILE_OFFSET64   
  4.     __ino_t d_ino;   
  5.     __off_t d_off;   
  6. #else   
  7.     __ino64_t d_ino;   
  8.     __off64_t d_off;   
  9. #endif   
  10.     unsigned short int d_reclen;   
  11.     unsigned char d_type;   
  12.     char d_name[256];       /* We must not include limits.h! */   
  13.   };   
  14.    
  15. #ifdef __USE_LARGEFILE64   
  16. struct dirent64   
  17.   {   
  18.     __ino64_t d_ino;   
  19.     __off64_t d_off;   
  20.     unsigned short int d_reclen;   
  21.     unsigned char d_type;   
  22.     char d_name[256];       /* We must not include limits.h! */   
  23.   };   
  24. #endif   
  25.    
  26. /* File types for `d_type'.  */   
  27. enum   
  28.   {   
  29.     DT_UNKNOWN = 0,   
  30. # define DT_UNKNOWN DT_UNKNOWN   
  31.     DT_FIFO = 1,   
  32. # define DT_FIFO    DT_FIFO   
  33.     DT_CHR = 2,   
  34. # define DT_CHR     DT_CHR   
  35.     DT_DIR = 4,   
  36. # define DT_DIR     DT_DIR   
  37.     DT_BLK = 6,   
  38. # define DT_BLK     DT_BLK   
  39.     DT_REG = 8,   
  40. # define DT_REG     DT_REG   
  41.     DT_LNK = 10,   
  42. # define DT_LNK     DT_LNK   
  43.     DT_SOCK = 12,   
  44. # define DT_SOCK    DT_SOCK   
  45.     DT_WHT = 14   
  46. # define DT_WHT     DT_WHT   
  47.   };