简体   繁体   中英

Sorting structure variables(3 ints) in file from smallest to largest C

I need to sort my 2 text files for my project. One of the text files is in student id, course id and score format and the other is in student id's format. I wrote the values in both of them with fprintf, so I am using fscanf while reading, but my function is not working correctly, can you tell me where is my mistake?

(I deleted the old code that didn't work because I continued on a different code)

Edit :I tried to create a code snippet based on the solution below, but when I enter the while loop, fscanf starts to get the wrong numbers. Can you look for where my mistake is?

Edit :After editing here, I did the fopen checks. There is no problem with them. The while loop closes after it runs once, I think, in the if fscanf parts, fscanf does not read the number correctly, so it exits the loop with break.

most likely broken part:

    FILE *index = fopen("index.txt", "r");
    FILE *record = fopen("record.txt", "r");
    if (!index)
        return;
    if (!record)
        return;
    int array[n][3];
    //int *array = (int *)malloc(n *3 sizeof(int));???
    int count = 0;
    int i=0,temp,id,course,score;
    while (1)
    {  
        if(count==n) break;
        if (fscanf(record, "%d", &id) != 1) break;
        if (fscanf(record, "%d", &course) != 1) break;
        if (fscanf(record, "%d", &score) != 1) break;
        array[count][0] = id;
        array[count][1] = course;
        array[count][2] = score;
        count++;
    }

the rest of the function for you to browse if the error is elsewhere:

void sort_for_bin_search(int n)
{
    FILE *index = fopen("index.txt", "r");
    FILE *record = fopen("record.txt", "r");
    if (!index)
        return;
    if (!record)
        return;
    int array[n][3];
    //int *array = (int *)malloc(n *3 sizeof(int));???
    int count = 0;
    int i=0,temp,id,course,score;
    while (1)
    {  
        if(count==n) break;
        if (fscanf(record, "%d", &id) != 1) break;
        if (fscanf(record, "%d", &course) != 1) break;
        if (fscanf(record, "%d", &score) != 1) break;
        array[count][0] = id;
        array[count][1] = course;
        array[count][2] = score;
        count++;
    }
     for (i = 1; i < n - 1; i++)
    {
        for (int j = 0; j < n - 1; j++)
        {
            if(array[i][0] > array [j][0])
            {
            temp=array[j][0];
            array[j][0] = array[i][0];
            array[i][0] = temp;

            temp=array[j][1];
            array[j][1] = array[i][1];
            array[i][1] = temp;

            temp=array[j][2];
            array[j][2] = array[i][2];
            array[i][2] = temp;
            }
            else if((array[i][0]==array[j][0])&&(array[i][1]>array[j][1]))
            {
            temp=array[j][0];
            array[j][0] = array[i][0];
            array[i][0] = temp;

            temp=array[j][1];
            array[j][1] = array[i][1];
            array[i][1] = temp;

            temp=array[j][2];
            array[j][2] = array[i][2];
            array[i][2] = temp;
            }

        }
    }
    fclose(record);
    fclose(index);
    FILE *index2 = fopen("index.txt", "w");
    FILE *record2 = fopen("record.txt", "w");
    for (i = 0; i < n; i++)
    {
        fprintf(index2,"%d\n",array[i][0]);
        fprintf(record2,"%d %d %d\n",array[i][0],array[i][1],array[i][2]);
        //printf("%d %d %d\n",array[i][0],array[i][1],array[i][2]);
    }
    //free(array);
    fclose(record2);
    fclose(index2);
}

It looks like you are doing a bubble sort, and in each iteration you read/write from disk. Disk operations are very slow. It is much easier and faster if you read once in to array. And then sort that single array.

Example:

void sort_for_bin_search(int n)
{
    //assumes that `n` is the number of lines in this file
    if (n < 1) return;
    FILE* fin = fopen("index.txt", "r");
    if (!fin)
        return;
    FILE* fout = fopen("record.txt", "w");
    if (!fout)
        return;

    int* arr = malloc(n * sizeof(int));
    if (!arr) return;
    int count = 0;
    while (1)
    {
        if (count == n)
            break;
        int id, course, score;
        if (fscanf(fin, "%d", &id) != 1) break;
        if (fscanf(fin, "%d", &course) != 1) break;
        if (fscanf(fin, "%d", &score) != 1) break;
        arr[count] = id;
        count++;
    }
    //add code for sorting arr
    for (int i = 0; i < count; i++)
        fprintf(fout, "%d\n", arr[i]);

    free(arr);
    fclose(fin);
    fclose(fout);
}

Then you can sort, for example using bubble sort.

Use printf to print the data on screen at each step, this will help with debugging.

void sort_for_bin_search(int n)
{
    FILE* fin = fopen("input_file.txt", "r");
    if (!fin)
    {
        printf("input error\n");
        return;
    }
    int array[n][3];
    int count = 0;
    while (1)
    {
        int id, course, score;
        if (count == n) break;
        if (fscanf(fin, "%d", &id) != 1) break;
        if (fscanf(fin, "%d", &course) != 1) break;
        if (fscanf(fin, "%d", &score) != 1) break;
        array[count][0] = id;
        array[count][1] = course;
        array[count][2] = score;
        count++;
    }
    n = count;
    printf("reading:\n");
    for (int i = 0; i < n; i++)
        printf("%d %d %d\n", array[i][0], array[i][1], array[i][2]);

    printf("\nsort\n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - 1 - i; j++)
        {
            if (array[j][0] > array[j + 1][0])
            {
                int temp;
                temp = array[j][0];
                array[j][0] = array[j + 1][0];
                array[j + 1][0] = temp;

                temp = array[j][1];
                array[j][1] = array[j + 1][1];
                array[j + 1][1] = temp;

                temp = array[j][2];
                array[j][2] = array[j + 1][2];
                array[j + 1][2] = temp;
            }
        }
    }
    fclose(fin);

    printf("sorted\n");
    for(int i = 0; i < n; i++)
        printf("%d %d %d\n", array[i][0], array[i][1], array[i][2]);

    printf("write to file\n");
    FILE* fout = fopen("output_file.txt", "w");
    if(!fout)
    {
        printf("output error\n");
        return;
    }
    for (int i = 0; i < n; i++)
        fprintf(fout, "%d %d %d\n", array[i][0], array[i][1], array[i][2]);
    fclose(fout);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM