Hey Whiteflags,
This is a kind of Frankenstein of crappy college notes I find it difficult to make sense of:
Code:
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(FILE *ifp, FILE *ofp, int list[], int n);
int main(int argc, char **argv)
{
char file_name[30];
FILE *file_ptr, *file_ptra;
printf("Enter the name of the file to open >");
scanf("%s",file_name);
file_ptr = fopen(file_name[1], "r");
file_ptra = fopen(file_name[2], "w");
if (file_ptr != NULL)
printf("File open OK\n");
else
printf("Error opening file %s\n", file_name);
bubble_sort(FILE *ifp, FILE *ofp, int list[], int n);
fclose(ifp);
fclose(ofp);
return 0;
}
void bubble_sort(FILE *ifp, FILE *ofp, int list[], int n)
{
int j, k;
boolean exchange_made;
int temp;
k = 0;
exchange_made = TRUE;
// Make up to n - 1 passes through
// array, exit early if no exchanges
// are made on previous pass
while ((k < n - 1) && exchange_made)
{
exchange_made = FALSE;
++k;
// Number of comparisons on kth pass
for (j = 0; j < n - k; ++j)
if (list[j] > list[j + 1])
{
// Exchange must be made
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
exchange_made = TRUE;
print_list(list, n);
}
}
}
I know its a complete mess, but basically I want to be able to open the file to change it, so I'm guessing the means I need "r" and "w", so once I've opened the file, I want to call my bubble sort function, alphabetise the text in the text file and create a new file. I know its a complete mess and I'm miles away from getting it, but even if you could point me in the right direction I'd really appreciate it. I hate our notes. Thanks again.