Advertisement
LilChicha174

Untitled

May 12th, 2022
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8.  
  9. typedef struct text text;
  10. struct text{
  11.     char info[105];
  12. };
  13.  
  14. int comp(const void* a, const void* b){
  15.     text* aa = (text*)a;
  16.     text* bb = (text*) b;
  17.     if(strcmp(aa->info, bb->info) > 0) return 1;
  18.     if(strcmp(aa->info, bb->info) < 0) return -1;
  19.     return 0;
  20.  
  21. }
  22.  
  23.  
  24. int find(char *path, FILE *output, int number){
  25.     char buf;
  26.     int index;
  27.     int j;
  28.     char new_path[255];
  29.     DIR *dir = opendir(path);
  30.     DIR *try_dir = NULL;
  31.     if (dir){
  32.         struct dirent *de = readdir(dir);
  33.         while(de){
  34.             for (j=0; j < 255;j++)
  35.                 new_path[j] = '\0';
  36.             if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0){
  37.                 strcpy(new_path, path);
  38.                 strcat(new_path, "/");
  39.                 strcat(new_path, de->d_name);
  40.                 if (opendir(new_path)){
  41.                     number = find(new_path, output, number);
  42.                 }
  43.                 else{
  44.                     if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0 && strstr(de->d_name, ".txt") && !strstr(de->d_name, "result")){
  45.                         FILE *file = fopen(new_path, "r");
  46.                         char temp[100];
  47.                         while(fgets(temp, 100, file)){
  48.                             fprintf(output, " %s", temp);
  49.                             number++;
  50.                         }
  51.                         fclose(file);
  52.                     }
  53.                 }
  54.             }
  55.             de = readdir(dir);
  56.         }
  57.         closedir(dir);
  58.         return number;
  59.     }
  60.     return -1;
  61. }
  62.  
  63. void sort(FILE *input, text **file, int number){
  64.     char buf;
  65.     int i;
  66.     for (i =0; i< number; i++){
  67.         fscanf(input, "%c", &buf);
  68.         fgets((*file)[i].info, 100, input);
  69.     }
  70.     qsort(*file, number, sizeof(text), comp);
  71. }
  72.  
  73.  
  74. int main(){
  75.     int number =0;
  76.     int i;
  77.     FILE *answer;
  78.     answer = fopen("./a.txt", "w+");
  79.     number = find(".", answer, number);
  80.     text *file = calloc(number + 5, sizeof(text));
  81.     fclose(answer);
  82.     answer = fopen("./a.txt", "r");
  83.     sort(answer, &file, number);
  84.     fclose(answer);
  85.     answer = fopen("./a.txt", "w");
  86.     for (i=0; i<number;i++){
  87.         printf("%s", file[i].info);
  88.     }
  89.     for (i=0; i<number;i++){
  90.         fprintf(answer, "%s", file[i].info);
  91.     }
  92.     fclose(answer);
  93.     free(file);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement