Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- typedef struct text text;
- struct text{
- char info[105];
- };
- int comp(const void* a, const void* b){
- text* aa = (text*)a;
- text* bb = (text*) b;
- if(strcmp(aa->info, bb->info) > 0) return 1;
- if(strcmp(aa->info, bb->info) < 0) return -1;
- return 0;
- }
- int find(char *path, FILE *output, int number){
- char buf;
- int index;
- int j;
- char new_path[255];
- DIR *dir = opendir(path);
- DIR *try_dir = NULL;
- if (dir){
- struct dirent *de = readdir(dir);
- while(de){
- for (j=0; j < 255;j++)
- new_path[j] = '\0';
- if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0){
- strcpy(new_path, path);
- strcat(new_path, "/");
- strcat(new_path, de->d_name);
- if (opendir(new_path)){
- number = find(new_path, output, number);
- }
- else{
- if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0 && strstr(de->d_name, ".txt") && !strstr(de->d_name, "result")){
- FILE *file = fopen(new_path, "r");
- char temp[100];
- while(fgets(temp, 100, file)){
- fprintf(output, " %s", temp);
- number++;
- }
- fclose(file);
- }
- }
- }
- de = readdir(dir);
- }
- closedir(dir);
- return number;
- }
- return -1;
- }
- void sort(FILE *input, text **file, int number){
- char buf;
- int i;
- for (i =0; i< number; i++){
- fscanf(input, "%c", &buf);
- fgets((*file)[i].info, 100, input);
- }
- qsort(*file, number, sizeof(text), comp);
- }
- int main(){
- int number =0;
- int i;
- FILE *answer;
- answer = fopen("./a.txt", "w+");
- number = find(".", answer, number);
- text *file = calloc(number + 5, sizeof(text));
- fclose(answer);
- answer = fopen("./a.txt", "r");
- sort(answer, &file, number);
- fclose(answer);
- answer = fopen("./a.txt", "w");
- for (i=0; i<number;i++){
- printf("%s", file[i].info);
- }
- for (i=0; i<number;i++){
- fprintf(answer, "%s", file[i].info);
- }
- fclose(answer);
- free(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement