Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <dirent.h>
- #include <string.h>
- #include <stdlib.h>
- int n = 0;
- void readalldir(char *dirname, char **txtdata){
- DIR *dir = opendir(dirname);
- struct dirent *fi;
- while(fi = readdir(dir)){
- if(fi->d_type==DT_DIR && fi->d_name[0]!='.'){
- int senlen = strlen(dirname);
- strcat(dirname, "/");
- strcat(dirname, fi->d_name);
- readalldir(dirname, txtdata);
- dirname[senlen] = '\0';
- }
- if(fi->d_type==DT_REG){
- int senlen = strlen(dirname);
- strcat(dirname, "/");
- strcat(dirname, fi->d_name);
- FILE *txt = fopen(dirname, "r");
- txtdata[n] = malloc(sizeof(char) * 50);
- fgets(txtdata[n], 50, txt);
- strcat(txtdata[n], "\n");
- n++;
- fclose(txt);
- dirname[senlen] = '\0';
- }
- }
- closedir(dir);
- }
- void freetxtdata(char **txtdata){
- int i;
- for(i = 0; i<n; i++){
- free(txtdata[i]);
- }
- free(txtdata);
- }
- void writefile(char **txtdata){
- FILE *txt = fopen("result.txt", "w");
- int i;
- for(i = 0; i<n; i++){
- fputs(txtdata[i], txt);
- }
- }
- int comper(const void *a, const void *b){
- long int aa = atoi(*((char**) a));
- long int bb = atoi(*((char**) b));
- if(aa > bb){
- return 1;
- }
- if(aa < bb){
- return -1;
- }
- else{
- return 0;
- }
- }
- int main(){
- char dirname[50] = "./root";
- char **txtdata = malloc(sizeof(char*) * 3000);
- readalldir(dirname, txtdata);
- qsort(txtdata, n, sizeof(char*), comper);
- writefile(txtdata);
- freetxtdata(txtdata);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement