Skip to main content
  1. The first call to read the student file

    The first call to read the student file
  2. The bubble sort

    The bubble sort
  3. The second call to read the student file, which may not be necessary.

    void listing_id() { int line = -1; char temp[500], temp1[500], temp2[500], temp3[500]; long long unlisted_id[50], listed_id[50]; char string[500], stringgg[500];

    filePtr = fopen("student_informations.txt", "r");
    while (fgets(string, sizeof(string), filePtr) != NULL) {
        if (line == -1) {
            //fputs(string,stdout);
        }
        else {
            fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &listed_id[line], temp, temp1);
            unlisted_id[line] = listed_id[line];
        }
        line++;
    }
    fclose(filePtr);
    long long hold;
    
    for (int i = 0; i < line - 2; i++) { //bubble sort
        for (int i = 0; i < line - 2; i++) {
            if (listed_id[i] > listed_id[i + 1]) {
                hold = listed_id[i + 1];
                listed_id[i + 1] = listed_id[i];
                listed_id[i] = hold;
            }
        }
    }
    
    int loop = 0;
    for (int i = 0; i < line - 1; i++) {
        filePtr = fopen("student_informations.txt", "r"); // opening and closing back so that while loop will work, and cursor will go  back to the line 1
    
        while (fgets(stringgg, sizeof(stringgg), filePtr) != NULL) {
            if (loop == 0 || loop == 1) {
                fputs(stringgg, stdout);
                loop++;
            }
            else {
                fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &unlisted_id[0], temp2, temp3);
                if (listed_id[i] == unlisted_id[0]) {
                    printf("%s %s %lld %s %s\n", temp, temp1, listed_id[i], temp2, temp3);
                    break;
                }
            }
        }
        fclose(filePtr);
    }
    

    }

    The second call to read the student file, which may not be necessary.
    void listing_id() {
        int line = -1;
        char temp[500], temp1[500], temp2[500], temp3[500];
        long long unlisted_id[50], listed_id[50];
        char string[500], stringgg[500];

        filePtr = fopen("student_informations.txt", "r");
        while (fgets(string, sizeof(string), filePtr) != NULL) {
            if (line == -1) {
                //fputs(string,stdout);
            }
            else {
                fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &listed_id[line], temp, temp1);
                unlisted_id[line] = listed_id[line];
            }
            line++;
        }
        fclose(filePtr);
        long long hold;

        for (int i = 0; i < line - 2; i++) { //bubble sort
            for (int i = 0; i < line - 2; i++) {
                if (listed_id[i] > listed_id[i + 1]) {
                    hold = listed_id[i + 1];
                    listed_id[i + 1] = listed_id[i];
                    listed_id[i] = hold;
                }
            }
        }

        int loop = 0;
        for (int i = 0; i < line - 1; i++) {
            filePtr = fopen("student_informations.txt", "r"); // opening and closing back so that while loop will work, and cursor will go  back to the line 1

            while (fgets(stringgg, sizeof(stringgg), filePtr) != NULL) {
                if (loop == 0 || loop == 1) {
                    fputs(stringgg, stdout);
                    loop++;
                }
                else {
                    fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &unlisted_id[0], temp2, temp3);
                    if (listed_id[i] == unlisted_id[0]) {
                        printf("%s %s %lld %s %s\n", temp, temp1, listed_id[i], temp2, temp3);
                        break;
                    }
                }
            }
            fclose(filePtr);
        }
    }
  1. The first call to read the student file

  2. The bubble sort

  3. The second call to read the student file, which may not be necessary.

    void listing_id() { int line = -1; char temp[500], temp1[500], temp2[500], temp3[500]; long long unlisted_id[50], listed_id[50]; char string[500], stringgg[500];

    filePtr = fopen("student_informations.txt", "r");
    while (fgets(string, sizeof(string), filePtr) != NULL) {
        if (line == -1) {
            //fputs(string,stdout);
        }
        else {
            fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &listed_id[line], temp, temp1);
            unlisted_id[line] = listed_id[line];
        }
        line++;
    }
    fclose(filePtr);
    long long hold;
    
    for (int i = 0; i < line - 2; i++) { //bubble sort
        for (int i = 0; i < line - 2; i++) {
            if (listed_id[i] > listed_id[i + 1]) {
                hold = listed_id[i + 1];
                listed_id[i + 1] = listed_id[i];
                listed_id[i] = hold;
            }
        }
    }
    
    int loop = 0;
    for (int i = 0; i < line - 1; i++) {
        filePtr = fopen("student_informations.txt", "r"); // opening and closing back so that while loop will work, and cursor will go  back to the line 1
    
        while (fgets(stringgg, sizeof(stringgg), filePtr) != NULL) {
            if (loop == 0 || loop == 1) {
                fputs(stringgg, stdout);
                loop++;
            }
            else {
                fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &unlisted_id[0], temp2, temp3);
                if (listed_id[i] == unlisted_id[0]) {
                    printf("%s %s %lld %s %s\n", temp, temp1, listed_id[i], temp2, temp3);
                    break;
                }
            }
        }
        fclose(filePtr);
    }
    

    }

  1. The first call to read the student file
  2. The bubble sort
  3. The second call to read the student file, which may not be necessary.
    void listing_id() {
        int line = -1;
        char temp[500], temp1[500], temp2[500], temp3[500];
        long long unlisted_id[50], listed_id[50];
        char string[500], stringgg[500];

        filePtr = fopen("student_informations.txt", "r");
        while (fgets(string, sizeof(string), filePtr) != NULL) {
            if (line == -1) {
                //fputs(string,stdout);
            }
            else {
                fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &listed_id[line], temp, temp1);
                unlisted_id[line] = listed_id[line];
            }
            line++;
        }
        fclose(filePtr);
        long long hold;

        for (int i = 0; i < line - 2; i++) { //bubble sort
            for (int i = 0; i < line - 2; i++) {
                if (listed_id[i] > listed_id[i + 1]) {
                    hold = listed_id[i + 1];
                    listed_id[i + 1] = listed_id[i];
                    listed_id[i] = hold;
                }
            }
        }

        int loop = 0;
        for (int i = 0; i < line - 1; i++) {
            filePtr = fopen("student_informations.txt", "r"); // opening and closing back so that while loop will work, and cursor will go  back to the line 1

            while (fgets(stringgg, sizeof(stringgg), filePtr) != NULL) {
                if (loop == 0 || loop == 1) {
                    fputs(stringgg, stdout);
                    loop++;
                }
                else {
                    fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &unlisted_id[0], temp2, temp3);
                    if (listed_id[i] == unlisted_id[0]) {
                        printf("%s %s %lld %s %s\n", temp, temp1, listed_id[i], temp2, temp3);
                        break;
                    }
                }
            }
            fclose(filePtr);
        }
    }
Source Link
pacmaninbw
  • 24.8k
  • 11
  • 46
  • 109

My initial impression when I was only visually scanning the code was Well Done! The code is very readable, the variable names and function names are clear and state the purpose of the variable or function clearly.

This code should not work.

Warnings from my compiler:
main.c(147,20): warning C4456: declaration of 'i' hides previous local declaration main.c(146,14): message : see declaration of 'i'

There is a least one warning I got that needs to be addressed, there is a serious problem in the function listing_id() the code below:

    for (int i = 0; i < line - 2; i++) { //bubble sort
        for (int i = 0; i < line - 2; i++) {
            if (listed_id[i] > listed_id[i + 1]) {
                hold = listed_id[i + 1];
                listed_id[i + 1] = listed_id[i];
                listed_id[i] = hold;
            }
        }
    }

The code is redefining the integer variable i in the inner loop, this nested loop needs 2 different loop control variables.

Another logic issue that I find troubling is that the function main_menu() is the main loop of the program, but there is a recursive call to it in a several lower level function, this makes the program very hard to trace the logic and find how you get to where you are:

void list_student() {

    int user_input;
    printf("%s", "\n\n1->List according to the Name\n"
        "2->List according to the ID\n"
        "3->Go back to previous menu\n");
    scanf("%d", &user_input);
    if (user_input == ACCORDING_NAME_LIST) {
        listing_name();
    }
    else if (user_input == ACCORDING_TO_ID) {
        listing_id();
    }
    else if (user_input == 3) {
        main_menu();
    }
    else {
        printf("%s", "Invalid input, try again");
        list_student();
    }
}

Only call the main_menu function once

General Review Comments

Avoid Global Variables

A best practice in most programming languages that require variable delcarations is to declare variables as they are needed. One of your global variables, enum user_inputs; is never used in the program. The other global variable, FILE* filePtr; does not seem to need to be a global variable since the file is opened and closed every time it is used, and can be declared locally.

It is very difficult to read, write, debug and maintain programs that use global variables. Global variables can be modified by any function within the program and therefore require each function to be examined before making changes in the code. In C and C++ global variables impact the namespace and they can cause linking errors if they are defined in multiple files. The answers in this stackoverflow question provide a fuller explanation.

Code Organization

Function prototypes are very useful in large programs that contain multiple source files, and that in case they will be in header files. In a single file program like this it is better to put the main() function at the bottom of the file and all the functions that get used in the proper order above main(). Keep in mind that every line of code written is another line of code where a bug can crawl into the code.

Complexity

There is a programming principle called the Single Responsibility Principle that applies here. The Single Responsibility Principle states:

that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by that module, class or function.

I see 3 separate functions that can be created in the following function.

  1. The first call to read the student file

  2. The bubble sort

  3. The second call to read the student file, which may not be necessary.

    void listing_id() { int line = -1; char temp[500], temp1[500], temp2[500], temp3[500]; long long unlisted_id[50], listed_id[50]; char string[500], stringgg[500];

    filePtr = fopen("student_informations.txt", "r");
    while (fgets(string, sizeof(string), filePtr) != NULL) {
        if (line == -1) {
            //fputs(string,stdout);
        }
        else {
            fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &listed_id[line], temp, temp1);
            unlisted_id[line] = listed_id[line];
        }
        line++;
    }
    fclose(filePtr);
    long long hold;
    
    for (int i = 0; i < line - 2; i++) { //bubble sort
        for (int i = 0; i < line - 2; i++) {
            if (listed_id[i] > listed_id[i + 1]) {
                hold = listed_id[i + 1];
                listed_id[i + 1] = listed_id[i];
                listed_id[i] = hold;
            }
        }
    }
    
    int loop = 0;
    for (int i = 0; i < line - 1; i++) {
        filePtr = fopen("student_informations.txt", "r"); // opening and closing back so that while loop will work, and cursor will go  back to the line 1
    
        while (fgets(stringgg, sizeof(stringgg), filePtr) != NULL) {
            if (loop == 0 || loop == 1) {
                fputs(stringgg, stdout);
                loop++;
            }
            else {
                fscanf(filePtr, "%s %s %lld %s %s", temp, temp1, &unlisted_id[0], temp2, temp3);
                if (listed_id[i] == unlisted_id[0]) {
                    printf("%s %s %lld %s %s\n", temp, temp1, listed_id[i], temp2, temp3);
                    break;
                }
            }
        }
        fclose(filePtr);
    }
    

    }

DRY Code

There is a programming principle called the Don't Repeat Yourself Principle sometimes referred to as DRY code. If you find yourself repeating the same code multiple times it is better to encapsulate it in a function. If it is possible to loop through the code that can reduce repetition as well. The function above may be violating this principle as well.