I have a 80370-long list of 30-digit numbers in a text file, each in a separate line.
I want to sort them in C.
My idea was creating a new file, then adding the first number to it.
Then, each time we compare it with another number. If it's smaller, the other number is appended. Easy. However, if it's larger, the new number must be prepended.
I tried prepending like so:
void prepend(char line[], FILE* w, FILE* waux, char filename[], char auxname[]) {
fprintf(waux, "%s\n", line); //print new number to new file (waux)
char ch;
while((ch = fgetc(w)) != EOF) {
fputc(ch, waux); //read old file (w) and add to new file (waux)
}
remove(filename); //delete old file
rename(auxname, filename); //rename new file to old file's name
}
But then I try to read the output and it's filled with NULL chars (in Notepad++).
Among the million NULL chars we can find the numbers, but they're not sorted at all.