0

This is a project for a class. My partner and I have been working on it for awhile and seem to be a bit stuck. The assignment tasked us to do the following:

  • Ask user for a directory name.
  • Ask user for a string of letters that represent the beginning of a file name.
  • Open the directory to find all of the files that begin with the string asked for in procedure #2.
  • Store each string to an array of linked lists. The array will have 26 elements, one element for each letter in the alphabet.
  • Sort the list in an adequate way before printing to output.

Well, here is our code so far. I know we need to work on the struct node* array and we've managed to find a better way to open directories. But other than that, the code seems to be failing. Any suggestions or pointers to where i'm messing up would be awesome! Thanks, Adam.

    #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);
    }
  • Welcome to SO, please add the flag `c` to increase the number of visitors. And what is exactly your problem, error ? Define where **"the code seems to be failing."** !!! Then did you use the debugger ? – J. Piquard Feb 20 '17 at 20:43
  • Did you have really take into account the long list of errors and warnings that your compiler will provide with the current source code ? – J. Piquard Feb 20 '17 at 20:52
  • alright, so some of the errors/warnings i've had a time with are: "expected 'struct node **' but argument is of type 'struct node * (*)[26]' int insert(struct node **array_header, int index, struct node *pointer)." – Chance Carmichael Feb 21 '17 at 00:55
  • the new insert function is this: int insert(struct node ***array_header, int index, struct node *pointer) {struct node *link = (struct node*) malloc(sizeof(struct node)); link -> next = head; head = link;} – Chance Carmichael Feb 21 '17 at 00:58
  • In your new insert() function, what is the link between`array_header` and `head` ? What is the need of `index` and `pointer` ? Please edit your question and append compilable extra source code with description of concret problems. – J. Piquard Feb 21 '17 at 07:11

0 Answers0