Thread: How store sorted values in a text file after using Bubble sort?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    12

    How store sorted values in a text file after using Bubble sort?

    I'm trying to create a program that reads a file, sort the values in ascending order using bubble sort and then update the file with the new sorted values. When I ran the code it prints the sorted values, but the text file is blank. I'm new to this type of programming technique and any help would be appreciated.


    Code:
    void bubbleSort(int myArray[]);
    
    
    int main(){
    
    
        FILE *fptr;
        char ptr[1000] = "numbers.txt";
        int myArray[500];
        int i;
    
    
        fptr = fopen(ptr,"r");
        if (fptr == NULL){
            printf("Error Reading File\n");
            exit(0);
        }
    
    
        for (i = 0; i < 7; i++){
            fscanf(fptr, "%d,", &myArray[i]);
        }
    
    
        for (i = 0; i < 7; i++){
            printf("Number: %d\n", myArray[i]);
        }
    
    
        printf("\n");
        bubbleSort(myArray);
        for (i = 0; i < 7; i++){
            printf("Sorted numbers: %d\n", myArray[i]);
            fptr = fopen(ptr,"w");
        }
    
    
        fclose(fptr);
    
    
        return 0;
    }
    
    
    void bubbleSort(int myArray[]){
        int i, j, temp;
        for(i = 0; i < 7 - 1; i++){
            for(j = 0; j < 7 - 1; j++){
                if (myArray[j] > myArray[j+1]){
                    temp = myArray[j];
                    myArray[j] = myArray[j+1];
                    myArray[j+1] = temp;
                }
            }
        }
    }
    Last edited by JonathanSimo; 09-27-2020 at 11:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    > Enable GingerCannot connect to Ginger Check your internet connection
    Please turn this crap off.

    > fptr = fopen(ptr,"w");
    1. You only need to do this once.
    2. You need close the file for reading, before you open it for writing.
    3. Look how you used fscanf - now do the same using fprintf.
    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
    Mar 2019
    Posts
    12
    Thanks mate, I got the program to work correctly. Appreciated the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-30-2013, 08:44 PM
  2. Replies: 13
    Last Post: 04-19-2012, 01:24 AM
  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