Home > Back-end >  How do I sort by number stored in a file in c?
How do I sort by number stored in a file in c?

Time:11-14

I want to read the data below and store it in a struct and sort it by oldest. And I want to write a struct with sorted data to a new file. (Sort in ascending order from oldest to youngest)

data:

24 Zachary Gordon
54 Cuba Gooding
67 Peter Killian Gallagher
36 Kyle Gallner

output that i want:

24 Zachary Gordon
36 Kyle Gallner
54 Cuba Gooding
67 Peter Killian Gallagher

my code:

#include <stdio.h>
#define N 4

struct Data {
    int age;
    char name[30];
};

void sortData(struct Data aa[]);

int main() {
    struct Data aa[N];
    struct Data tmp;

    FILE *input, *output;
    
    input = fopen("data.txt", "r");
    output = fopen("output.txt", "w");

    if (input == NULL) {
        printf("Fail to read file");
    }
    
    for (int i = 0; i < N; i  ) {
        fscanf(input, "%d", &aa[i].name);
        fgets(aa[i].name, 30, input);

    }
    
    for (int i = 0; i < N; i  ) {
        printf("%d %s", aa[i].age, aa[i].name);
    }

    fclose(input);

    sortData(aa);

    for (int i = 0; i < N; i  ) {
        fprintf(output, "%d %s", aa[i].age, aa[i].name);
    }

    return 0;
}

void sortData(struct Data aa[]) {
    struct Data tmp;

    for (int i = 0; i < N - 1; i  ) {
        for (int j = i   1; j < N; j  ) {
            if (aa[i].age > aa[j].age) {
                tmp = aa[i];
                aa[i] = aa[j];
                aa[j] = tmp;
            }
        }
    }
}

I don't know what went wrong. Thanks in advance to those who will reply

CodePudding user response:

When trying out your code, I got some warnings referring to the scanning in of data from the data.txt file and also did not see where the code actually stores the age. With that in mind, I made a few tweaks to the code as follows:

#include <stdio.h>

#define N 4

struct Data {
    int age;
    char name[30];
};

void sortData(struct Data aa[]);

int main() {
    struct Data aa[N];
    //struct Data tmp;  /* Don't need this here.  It is defined within the function as a local structure */

    char data[65];

    FILE *input, *output;

    input = fopen("data.txt", "r");
    output = fopen("output.txt", "w");

    if (input == NULL) {
        printf("Fail to read file");
    }

    for (int i = 0; i < N; i  ) {
        fgets(data, 64, input);                             /* Read in the full line first */
        sscanf(data, "%d %[^\n]", &aa[i].age, aa[i].name);  /* Then parse it to separate and store the age and name */
    }

    for (int i = 0; i < N; i  ) {
        printf("%d %s\n", aa[i].age, aa[i].name);
    }

    fclose(input);

    sortData(aa);

    for (int i = 0; i < N; i  ) {
        fprintf(output, "%d %s\n", aa[i].age, aa[i].name);
    }

    return 0;
}

void sortData(struct Data aa[]) {
    struct Data tmp;

    for (int i = 0; i < N - 1; i  ) {
        for (int j = i   1; j < N; j  ) {
            if (aa[i].age > aa[j].age) {
                tmp = aa[i];
                aa[i] = aa[j];
                aa[j] = tmp;
            }
        }
    }
}

Some items to point out.

  • Instead of using fscanf to read in a line of data from the file, the fgets function was used instead to acquire the complete data line.
  • With the full line of data inputted, the sscanf function was called to parse the line and store the age into the age element of the structure and then the name into the name element of the structure.

With those tweaks and creating a text file with your data, the following data was viewed at the terminal.

@Dev:~/C_Programs/Console/StructureSort/bin/Release$ ./StructureSort 
24 Zachary Gordon
54 Cuba Gooding
67 Peter Killian Gallagher
36 Kyle Gallner

Give that a try and see if it meets the spirit of your project.

  • Related