Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <dirent.h>
- char* txtOpen(char* txtName){
- FILE* txt = fopen(txtName, "r");
- if (txt == NULL){
- printf("ERROR\n");
- return NULL;
- }
- fseek(txt,0,SEEK_END);
- int txtSize = ftell(txt);
- fseek(txt,0,SEEK_SET);
- if(txtSize == 0){
- fclose(txt);
- return NULL;
- }
- char* txtStr = (char*)malloc(sizeof(char)*10000);
- fgets(txtStr, txtSize*sizeof(char),txt);
- fclose(txt);
- return txtStr;
- }
- void list_dir(const char* dirName, char** strsTxt, int* len){
- char current_path[10000];
- strcpy(current_path, dirName);
- DIR *current_dir = opendir(current_path);
- if (current_dir == NULL) return;
- struct dirent* current_dir_file =readdir(current_dir);
- while(current_dir_file){
- int path_len = strlen(current_path);
- strcat(current_path, "/");
- strcat(current_path, current_dir_file->d_name);
- if(current_dir_file->d_type == DT_REG && strstr(current_dir_file->d_name, ".txt") != NULL){
- if((strsTxt[*len] = (char*)txtOpen(current_path)) != NULL)
- (*len)++;
- }
- if (current_dir_file->d_type == DT_DIR && strcmp(".", current_dir_file->d_name) != 0 && strcmp("..",current_dir_file->d_name) != 0) list_dir(current_path,strsTxt,len);
- current_path[path_len] = '\0';
- current_dir_file = readdir(current_dir);
- }
- closedir(current_dir);
- }
- int compare(const void* a, const void* b){
- int a_value = atoi(*(char**)a);
- int b_value = atoi(*(char**)b);
- if (a_value > b_value)
- return 1;
- if (a_value < b_value)
- return -1;
- return 0;
- }
- int main(){
- char** strs = (char**)malloc(sizeof(char*)*1000);
- int len = 0;
- int i=0;
- char str[1000];
- FILE* fo;
- fo=fopen("fo","w");
- list_dir(".",strs,&len);
- qsort(strs, len, sizeof(char*), compare);
- for (i=0;i<len;i++) fprintf(fo,"%s\n", strs[i]);
- fclose(fo); //закрываем
- fo=fopen("fo","r");
- while (fgets(str, 1000, fo))
- printf("%s", str);
- fclose(fo);
- for (i=0;i<len;i++) free(strs[i]);
- free(strs);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement