一个程序,它询问字符串并读取目录以查找以该字符串开头的所有文件

时间:2017-02-20 19:37:25

标签: c arrays linked-list directory search-suggestion

这是一个班级的项目。我和我的伙伴已经研究了一段时间,似乎有点卡住了。该任务要求我们执行以下操作:

  • 询问用户目录名称。
  • 向用户询问一串代表文件名开头的字母。
  • 打开目录以查找以过程#2中要求的字符串开头的所有文件。
  • 将每个字符串存储到链接列表数组中。该数组将包含26个元素,字母表中每个字母一个元素。
  • 在打印到输出之前以适当的方式对列表进行排序。

嗯,这是我们的代码到目前为止。我知道我们需要处理struct node * array,我们设法找到一种更好的方法来打开目录。但除此之外,代码似乎失败了。任何建议或指向我搞砸的地方都会很棒! 谢谢,亚当。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #include <dirent.h>

    //THE LINKED LIST STRUCTURE.
    struct node
    {
        char data[50];                  //THE DATA.
        int key;                        //THE KEY.
        struct node *next;              //POINT TO NEXT NODE.
    };    
    struct node *head = NULL;           //THE FIRST NODE.
    struct node *current = NULL;        //THE CURRENT NODE.

    //PRINT EACH ITEM IN THE LIST.
    void print_list()
    {
        //SET THE POINTER TO THE FIRST ELEMENT OF THE LIST.
        struct node *pointer = head;
        //WHILE THE POINTER IS NOT NULL, IE WHEN THE LIST IS NOT AT THE  
        //END....
        while(pointer != NULL)
        {
            //PRINT THE DATA AT THE CURRENT POINTER.
            //ADVANCE THE POINTER TO THE NEXT NODE.
            printf("%s \n", pointer -> data);
            pointer = pointer -> next;
        }
    }

    //INSERT NODE AT HEAD.
    void insert(int key, char data[])
    {
        //CREATE A LINK, ALLOCATE MEMORY EQUAL TO THE SIZE OF A NODE  
        //STRUCTURE.
        //SET THE LINK KEY TO THE KEY PARAMETER.
        //SET THE LINK DATA TO THE DATA PARAMETER.
        //POINT TO PREVIOUS FIRST NODE.
        //POINT TO NEW FIRST NODE.
        struct node *link = (struct node*) malloc(sizeof(struct node));
        link -> key = key;
        link -> data = data[];
        link -> next = head;
        head = link;
    } 

    //DELETE NODE AT HEAD.
    struct node* delete()
    {
        //CREATE A POINTER AND SET IT TO THE FIRST NODE.
        //POINT TO THE NEXT NODE AND SET IT AS THE HEAD.
        //RETURN THE EMPTY NODE.
        struct node *temp_link = head;
        head = head -> next;
        return temp_link;
    }

    //SORT LIST BY HEADER.
    void sort_list()
    {
        int i, j, k;                //LOOP COUNTERS.
        int temp_key, temp_data;    //TEMPORARY KEY AND DATA HOLDER 
                                    //VARIABLES.
        struct node *current;       //POINTER TO THE CURRENT NODE.
        struct node *head;          //POINTER TO THE FIRST NODE.   
        struct node *next;          //POINTER TO THE NEXT NODE.
        int size = length();        //SET SIZE TO LIST LENGTH.         
        k = size;                   //LOOP COUNTER K = LIST SIZE.

        //FOR EVERY ITEM IN THE LIST....
        for(int i = 0; i < size - 1; i++, k--)
        {
            //SET THE CURRENT POINTER TO THE HEAD.
            //SET THE NEXT POINTER TO THE NEXT NODE.
            //INCREMENT I.
            //DECREMENT K.
            current = head;
            next = head -> next;

            //FOR EVERY ITEM IN THE LIST....
            for(j = 1; j < k; j++)
            {
                //IF THE CURRENT DATA > THE NEXT DATA
                if(current -> data > next -> data)
                {
                    //STORE THE CURRENT DATA IN TEMP.
                    //SET THE NEXT DATA TO CURRENT DATA.
                    //SET THE CURRENT DATA TO NEXT DATA.
                    temp_data = (int)current -> data;
                    current -> data = next -> data;
                    next -> data = temp_data;

                    //STORE THE CURRENT KEY IN TEMP.
                    //SET THE NEXT KEY TO CURRENT KEY.
                    //SET THE CURRENT KEY TO NEXT KEY.
                    temp_key = current -> key;
                    current -> key = next -> key;
                    next->key = temp_key;
                }
                //ADVANCE THE CURRENT POINTER.
                //ADVANCE THE NEXT POINTER.
                //INCREMENT J.
                //TEST IF J < K.
                current = current -> next;
                next = next -> next;
            }
        }
    }

    //RETURN LENGTH OF THE LIST.
    int length()
    {
        //INITIALIZE LENGTH TO 0.
        //CREATE A POINTER TO THE CURRENT NODE.
        int length = 0;
        struct node *current;

        //START AT THE HEAD. IF THE CURRENT POINTER IS POINTING TO 
        //SOMETHING....
        for(current = head; current != NULL; current = current -> next)
        {
            //INCREMENT LENGTH.
            //ADVANCE THE POINTER.
            length++;
        }
        //RETURN THE LENGTH COUNT.
        return length;
    }

    //DETERMINE IF LIST IS EMPTY.
    bool empty()
    {
        //IF THE FIRST NODE POINTS TO NOTHING, LIST IS EMPTY.
        return head == NULL;
    }

    //REVERSE THE LIST
    void reverse(struct node** head_ref)
    {
        //POINTER TO THE PREVIOUS NODE.
        //POINTER TO THE CURRENT NODE.
        //POINTER TO THE NEXT NODE.
        struct node* previous = NULL;
        struct node* current = *head_ref;
        struct node* next;

        //WHILE THE CURRENT NODE POINTS TO SOMETHING....
        while(current != NULL)
        {
            //SET THE NEXT NODE TO THE NEXT NODE.
            //SET THE CURRENT NODE POINTER TO THE PREVIOUS NODE.
            //SET THE PREVIOUS NODE TO THE CURRENT NODE.
            //SET THE NEXT NODE TO THE PREVIOUS NODE.
            next = current -> next;
            current -> next = previous;
        previous = current;
        current = next;
    }
    //SET THE HEAD REFERENCE TO THE PREVIOUS NODE.
    *head_ref = previous;
    }

    int main(int argc, char** argv) 
    {
        //VARIABLE DECLARATION.
        char folder[30];        //FOLDER NAME.
        char file_name[20];     //THE ENTERED FILE NAME BEGINNING.
        char buffer[50];        //FILE NAME BEGINNING COMPARATOR.
        int sort[26];           //THE ARRAY OF FILE NAME HEADERS.
        int count;              //AN INTEGER TRACKER.
        int temp_int;           //ASCII VALUE OF ARRAY HEADER.
        char temp;              //TEMPORARY CHARACTER HOLDER.

    //INITIALIZATION.
    char working_directory[30] = ".";   //SET THE DIRECTORY NAME.
    DIR *directory_pointer = NULL;      //POINTER TO A DIRECTORY.
    struct dirent *pent = NULL;         //DIRECTORY POINTER.
    count = 0;                          //KEEPS TRACK OF CHARACTER INDEXES.

    //PROMPT FOR THE FOLDER NAME.
    //GET THE FOLDER NAME.
    //FIND THE PATH OF THE FOLDER.
    //SET THE DIRECTORY POINTER TO THE PATH
    printf("Enter a folder name: "); 
    gets(folder);
    strcat(working_directory, folder);
    directory_pointer = opendir(folder);

    //WHILE THE STRING LENGTH ISN'T 0....
    do
    {
        //RESET COUNT.
        //PROMPT FOR THE BEGINNING OF A FILE NAME.
        //GET THE STRING FROM STANDARD INPUT.
        count = 0;
        printf("\nEnter the beginning of a filename: ");
        gets(file_name);

        //IF THE LENGTH OF THE ENTERED STRING IS NOT 0.
        if(strlen(file_name) > 0)
        {
            //HOLD THE FIRST LETTER OF THE FILE NAME IN TEMP.
            //LOWER CASE THAT LETTER.
            //SET THE COMPARATOR TO A LINE IN THE DIRECTORY.
            temp = file_name[0];
            temp_int = tolower(temp);
            buffer[0] = pent;

            //OUTPUT HEADER.
            printf("Files starting with ""%s"" in ""%s"":\n", file_name,  
            folder);

            //WHILE THERE EXISTS FILES TO READ....
            while(pent = readdir(directory_pointer))
            {
                //FOR EACH LETTER IN THE FILE NAME STRING....
                for(int i = 0; i < strlen(file_name); i++)
                {
                    //COMPARE FILE NAME WITH COMPARATOR.
                    if(file_name[i] == buffer[i]) 
                    {
                        //INCREMENT COUNT IF LETTERS MATCH
                        count++;
                    }
                }

                //IF THE FILE NAME AND COMPARATOR MATCH.
                if(strlen(file_name) == count)
                {
                    //INSERT THE LINE OF THE DIRECTORY IN THE APPROPRIATE ARRAY INDEX.
                    insert(temp_int, pent);
                }
            }

                //SORT THE LIST.
                //PRINT THE LIST.
                sort_list();
                print_list();
            }

            //IF STRING ENTERED IS EMPTY, CLOSE THE DIRECTORY AND QUIT THE    
            //PROGRAM SUCCESSFULLY!
            else
                printf("Successfully quit!\n");
                closedir(working_directory);
                return(EXIT_SUCCESS);

        }while(strlen(file_name) > 0); 

        return (EXIT_SUCCESS);
    }

0 个答案:

没有答案