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;
}
}
}
}