Question: Is there something wrong with my code? The code compiles fine but I can't seem to read my .txt files properly. The goal of the
Is there something wrong with my code? The code compiles fine but I can't seem to read my .txt files properly. The goal of the program is to take the other two .txt files and put them into the 3rd one where they are sorted from least to greatest. here is my code
#include
int main(int argc, char *argv[]) {
//Pointer for file read
if(argc!=4) { printf("Command arguments are not proper "); //error message incase invaild input
exit(0); }
FILE *list1,*list2,*sort;
list1 = fopen(argv[1],"r"); list2 = fopen(argv[2],"r"); sort = fopen(argv[3],"w");// sort.txt file so 'w'
int num1,num2;
fscanf (list1, "%d", &num1); fscanf (list2, "%d", &num2);
while (!feof(list1) && !feof(list2)) { if (num1 fprintf(sort,"%d ",num1);// print num 1 num1 = -9999; fscanf (list1, "%d", &num1); // read num1 } else { fprintf(sort,"%d ",num2); num2 = -9999; fscanf (list2, "%d", &num2); // read num2 } } if (!feof(list1)) { while (!feof(list1)) { fprintf(sort,"%d ",num1);// print num 1 num1 = -9999; fscanf (list1, "%d", &num1); // read num1 } } else { while (!feof(list2)) { fprintf(sort,"%d ",num2); num2 = -9999; fscanf (list2, "%d", &num2); // read num2 } } if(num1!=-9999 && num2!=-9999) { if(num1 } else { fprintf(sort,"%d ",num2);// print num 2 fprintf(sort,"%d ",num1); } } if(num1!=-9999) { fprintf(sort,"%d ",num1);// print num 1 } if(num2!=-9999) { fprintf(sort,"%d ",num2);// print num 2 } fclose(list1);// closing file list1 fclose(list2);// closing file list2 fclose(sort);// closing file sort return 0; return 0; } here is the files list1.txt 43 27 66 3352 20 list2.txt 600 24242 345 67 the last txt file, sort.txt, should be the one where its all printed and sorted but it seems to never happen.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
