Thread: Hot to bubble sort records from dat file

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    2

    Hot to bubble sort records from dat file

    Hello everyone! I'm trying to solve this particular problem that has been buggin me for a while, because I can't seem to find what i need anywhere on the net. I need to sort some records (6 or 7) read from a dat file using the bubble sort algorithm. I have written some record on file in "w" mode. Now, provided that the following is the code for normally reading my records:
    Code:
    Code:
    FILE * lettura;
    
    
    struct struttura{
        char nome[20];
        int anni;
        int punti;
        int numero;
    };
    
    
    struct struttura stru;
    
    
    int main()
    {
         lettura = fopen ("dati.dat", "r");
            if (lettura == NULL){
    
    
                fprintf(stderr, "\nError opening file\n");
                exit (1);
            }else{
                while(fread(&stru, sizeof(struct struttura), 1, lettura)){
                    printf("dati %s, %d, %d, %d\n", stru.nome, stru.anni, stru.punti, stru.numero);
                }
            }
    
    
        fclose(lettura);
    }
    How can i apply the sorting algorithm here? Can anyone show me the code?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first step would be to read all the records into an array.

    Do you need to write your own bubble sort (which is an awful algorithm for sorting by the way), or can you use the standard library function qsort ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    2
    I know BS is not very efficient but i need for very few records, so i guess it is acceptable. Any solution would work actually, as long as i make it work and understand how, so that i can apply it to other exercises.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, so start with
    Code:
    struct struttura stru[20];
    and fix the code you have to read each record into successive array elements, then using another loop, print out the valid records you've read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sort Txt file using structs
    By deburca98 in forum C Programming
    Replies: 8
    Last Post: 01-10-2021, 10:36 PM
  2. Replies: 2
    Last Post: 09-27-2020, 12:44 PM
  3. Bubble sort text file
    By decxan in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2011, 10:20 PM
  4. Bubble Sort from Input File
    By creilly in forum C Programming
    Replies: 11
    Last Post: 10-12-2010, 01:31 AM
  5. Rate My Application: File Stream/Bubble Sort
    By KingZoolerius66 in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2003, 11:04 PM

Tags for this Thread