Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<dirent.h>
- #include<sys/types.h>
- #include<string.h>
- #include<ctype.h>
- int comp(const void *a, const void *b);
- typedef struct Node_dir
- {
- struct Node_dir *next_dir;
- struct Node_dir *prev_dir;
- char *dir;
- }
- Node_dir;
- Node_dir* next_dir(Node_dir* head, char* dir)
- {
- while (head->next_dir != NULL)
- head = head->next_dir;
- Node_dir* push_dir = (Node_dir*) malloc (sizeof(Node_dir));
- push_dir->next_dir = NULL;
- head->next_dir = push_dir;
- push_dir->prev_dir = head;
- push_dir->dir = (char*) malloc(strlen(head->dir) +1+ strlen(dir) + 1);
- strcpy(push_dir->dir, head->dir);
- if (strcmp(head->dir, "/") == 0){
- strcat(push_dir->dir, dir);
- }
- else{
- strcat(push_dir->dir, "/");
- strcat(push_dir->dir, dir);
- }
- return push_dir;
- }
- void prev_dir(Node_dir* head)
- {
- while (head->next_dir != NULL)
- head = head->next_dir;
- free(head->dir);
- head->prev_dir->next_dir = NULL;
- head->prev_dir = NULL;
- }
- void rec_check_dir(Node_dir* cur_dir, FILE* log, FILE* texts)
- {
- fprintf(log, "Current direct - %s\n", cur_dir->dir);
- DIR* cur_open_direct = opendir(cur_dir->dir);
- if (!cur_open_direct)
- {
- fprintf(log, "\nCant open direct %s\n", cur_dir->dir);
- return;
- }
- struct dirent* entry;
- while ((entry = readdir(cur_open_direct)))
- {
- long entry_name_len = strlen(entry->d_name);
- if (entry->d_type == DT_REG &&
- entry->d_name[entry_name_len - 1] == 't' &&
- entry->d_name[entry_name_len - 2] == 'x' &&
- entry->d_name[entry_name_len - 3] == 't' &&
- entry->d_name[entry_name_len - 4] == '.')
- {
- fprintf(log, "filename and dir - %s/%s\n", cur_dir->dir, entry->d_name);
- char *file = (char*) malloc (strlen(cur_dir->dir)+1+strlen(entry->d_name) + 1);
- strcpy(file, cur_dir->dir);
- strcat(file, "/");
- strcat(file, entry->d_name);
- FILE* open_file = fopen(file, "r");
- char * str_from_file = (char*) malloc (256);
- fgets(str_from_file, 256, open_file);
- while (!feof(open_file))
- {
- fprintf(texts, "%s", str_from_file);
- fgets(str_from_file, 256, open_file);
- }
- fclose(open_file);
- }
- // else if (entry->d_type == DT_DIR &&
- // strcmp(entry->d_name, "..") &&
- // strcmp(entry->d_name, "."))
- // {
- // Node_dir* next = next_dir(cur_dir, entry->d_name);
- // // fprintf(log, "Next direct %s\n\n", next->dir);
- // rec_check_dir(next, texts);
- // prev_dir(next);
- // free(next);
- // }
- }
- fprintf(log, "Current dir is finished %s\n", cur_dir->dir);
- closedir(cur_open_direct);
- return;
- }
- int main()
- {
- Node_dir* head_direct = (Node_dir*) malloc (sizeof(Node_dir));
- head_direct->next_dir = NULL;
- head_direct->prev_dir = NULL;
- char* path = "/home/leo11/Projects/C/text_files";
- long len = strlen(path);
- head_direct->dir = (char*) malloc (len +1);
- strcpy(head_direct->dir, path);
- FILE* log = fopen("lllllog.txt", "w");
- // FILE* texts = fopen("ssssssssss.txt", "w");
- char** texts = (char**) malloc (sizeof(char*));
- long count_str = 0;
- rec_check_dir(head_direct, log, texts, count);
- fclose(texts);
- FILE* _texts = fopen("ssssssssss.txt", "r");
- fseek(_texts, 0, SEEK_END);
- long int pos = ftell(_texts);
- if (pos > 0)
- rewind(_texts);
- else
- exit(EXIT_SUCCESS);
- char str_from_file[256];
- char** array_str = (char**) malloc (sizeof(char*));
- int counter = 0;
- while (fgets(str_from_file, 256, _texts) != NULL)
- {
- array_str[counter] = (char*) malloc (strlen(str_from_file)+1);
- strcpy(array_str[counter], str_from_file);
- counter++;
- array_str = (char**) realloc(array_str, (counter+1)* sizeof(char*));
- }
- fclose(_texts);
- qsort(array_str, counter, sizeof(char*), comp);
- FILE* result = fopen("vvvvvvvvvvv.txt", "w");
- int i;
- for(i = 0; i < counter; i++)
- fprintf(result, "%s", array_str[i]);
- fclose(result);
- for(i = 0; i < counter; i++)
- free(array_str[i]);
- free(array_str);
- return 0;
- }
- int comp(const void *a, const void *b)
- {
- char* aa = *(char**) a;
- char* bb = *(char**) b;
- int count_c1 = 0;
- int count_c2 = 0;
- char *caa = aa;
- char* cbb = bb;
- while (*caa++)
- count_c1 += (int)(*caa);
- while (*cbb++)
- count_c2 += (int)(*cbb);
- if (count_c1 > count_c2)
- return 1;
- else if (count_c1 < count_c2)
- return -1;
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement