Untitled

 avatar
unknown
plain_text
3 years ago
3.7 kB
1
Indexable
#include <stdio.h>
#include <string.h>
#include <dirent.h>

void traverseDirectories(const char* directory_name, char gender[1], char firstname[20], char lastname[20]){

    DIR* dir = opendir(directory_name);
    int totalidx, startidx, endidx, z;

    if(dir == NULL){
        return;
    }

    struct dirent* entity;
    entity = readdir(dir);
    while(entity != NULL){
        if(entity->d_type == DT_DIR && strcmp(entity->d_name, ".") != 0 && strcmp(entity->d_name, "..") != 0){
            char path[100] = {0};
            strcat(path, directory_name);
            strcat(path, "/");
            strcat(path, entity->d_name);
            traverseDirectories(path, gender, firstname, lastname);
            //printf("%s\n", entity->d_name);
        }

        else if(entity->d_type == DT_REG){
            FILE *entity_file;
            entity_file = fopen(entity->d_name, "r");
            char buffer[256];
            totalidx = 0;


            while(fgets(buffer, 256, entity_file)){
                startidx = 0;
                endidx = 0;

                for (int i = 0; i < strlen(buffer); i++){

                    if(buffer[i] == ' '){

                        char currentword[20];
                        char currentword2[20];
                        z = 0;

                        for(int j = startidx; j < i; j++){
                            currentword[z] = buffer[j];
                            z++;
                        }
                        if(strcmp(currentword, firstname) == 0){
                            if(gender[0] == 'm' && buffer[startidx - 3] == 's'){
                                fseek(entity_file, totalidx + startidx - 3, SEEK_SET);
                                fputs('r', entity_file);
                            }
                            if(gender[0] == 'f' && buffer[startidx - 3] == 'r'){
                                fseek(entity_file, totalidx + startidx - 3, SEEK_SET);
                                fputs('s', entity_file);
                            }
                            z = 0;
                            for(int k = i + 1; i < i + 1 + strlen(lastname); k++){
                                currentword2[z] = buffer[k];
                                z++;
                            }

                            if(strcmp(currentword2, lastname) != 0){
                                fseek(entity_file, totalidx + i + 1, SEEK_SET);
                                fputs(lastname, entity_file);
                            }
                        }
                        startidx = i + 1;
                    }
                }

                totalidx += strlen(buffer);
            }
            fclose(entity_file);
        }

        entity = readdir(dir);
    }
    closedir(dir);
}

int main(int argc, char* argv[]){

    FILE *database_file;
    database_file = fopen("database.txt", "r");
    char buffer[256];
    int startidx, endidx, j;
    

    while(fgets(buffer, 256, database_file)){
        
        
        char gender[1];
        char firstname[20];
        char lastname[20];

        gender[0] = buffer[0];
        startidx = 2;
        endidx = 2;
        while(buffer[endidx] != ' '){
            endidx++;
        }

        j = 0;
        for (int i = startidx; i < endidx; i++)
        {
            firstname[j] = buffer[i];
            j++;
        }
        

        j = 0;
        for (int i = endidx + 1; i < strlen(buffer); i++){
            lastname[j] = buffer[i];
            j++;
        }
        traverseDirectories(".", gender, firstname, lastname);
    }
    

    fclose(database_file);

    

    
return 0;
}