I am struggling with writing a C program that sorts numbers read from a number of files, in ascending order.
Requirements:
- create 3 files (file1, file2, file3)
- get numbers from the user and in file1
- find the smallest number (in file1) and copy it to file3
- copy the numbers from file1 to file2 except the smallest number that we copied to file3.
- copy the numbers in file2 to file1 (removing the all old numbers).
- using while function to repeat the steps above until we sort all numbers in ascending order in file3.
- copy numbers from file3 to file1 and print all the numbers on the screen (from file1).
Note: The program must be using only files to sort numbers and shouldn't use any array.
I am working on this for hours but now and sometimes it works and sometimes it does not. I don't really know what's wrong.
Any ideas or suggestions on solving this problem?
#include <conio.h>
#include <stdio.h>
// to save the position of the smallest number
int pos;
// to get numbers from the user and save them in file1
void getNumbers(FILE *file) {
char x[255];
printf("Enter numbers: ");
scanf("%[^\n]s", &x);
fprintf(file, "%s", x);
}
// to find the smallest number and copy it to file3
void findSmallestNo(FILE *file1, FILE *file3) {
int x, temp;
rewind(file1);
fscanf(file1, "%d", &temp);
pos = ftell(file1);
while (!(feof(file1))) {
fscanf(file1, "%d", &x);
if (x < temp) {
temp = x;
pos = ftell(file1);
}
}
fprintf(file3, "%d ", temp);
}
// to copy numbers from file1 to file2 except the smallest number that we copied
// in the file3
void copyToFile2(FILE *file1, FILE *file2) {
int cur_pos, x;
rewind(file1);
rewind(file2);
while (!(feof(file1))) {
fscanf(file1, "%d", &x);
cur_pos = ftell(file1);
if (cur_pos != pos) {
fprintf(file2, "%d ", x);
}
}
}
// to copy from file2 to file1 (it should delete the old data in file1)
void copyToFile1(FILE *file1, FILE *file2) {
int x, count = 0;
rewind(file2);
while (!feof(file2)) {
count++;
fscanf(file2, "%d", &x);
fprintf(file1, "%d ", x);
}
}
// to print numbers on the screen
void print_file(FILE *file1) {
int x;
rewind(file1);
printf("\nSorted Numbers: ");
while ((fscanf(file1, "%d", &x)) == 1) {
printf("%d ", x);
}
printf("\n");
}
int main() {
int len, count = 1;
FILE *pFile1, *pFile2, *pFile3;
pFile1 = fopen("file1.txt", "w+");
pFile2 = fopen("file2.txt", "w+");
pFile3 = fopen("file3.txt", "a+");
if (pFile1 == NULL) {
printf("Couldn't open the file\n");
exit(1);
}
// to get the count of numbers in file1
getNumbers(pFile1);
int getLength(FILE * file) {
int nn, counting = 1;
rewind(file);
while ((fscanf(file, "%d ", &nn) == 1)) {
counting++;
}
return counting;
}
len = getLength(pFile1);
// while loop until var count is equal to the count of numbers in file1
while (count < len) {
++count;
findSmallestNo(pFile1, pFile3);
copyToFile2(pFile1, pFile2);
fclose(pFile1);
pFile1 = fopen("file1.txt", "w+");
copyToFile1(pFile1, pFile2);
}
fclose(pFile1);
fclose(pFile3);
pFile1 = fopen("file1.txt", "w+");
pFile3 = fopen("file3.txt", "r");
int number;
// to copy numbers from file3 to file1
while (count != 1) {
fscanf(pFile3, "%d ", &number);
fprintf(pFile1, "%d ", number);
--count;
}
// to print numbers on the screen in file1
print_file(pFile1);
fclose(pFile1);
fclose(pFile2);
fclose(pFile3);
return 0;
}
while(!(feof(file1))){
looks wrong. – MikeCAT