Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void read_numbers(FILE* fp1, FILE* fp2, FILE* fp3);
- void swap(int* a, int* b);
- int main(void)
- {
- // read all file at once
- FILE *fp1 = fopen("numbers1.txt", "r");
- FILE *fp2 = fopen("numbers2.txt", "r");
- // Open file to store the result
- FILE *fp3 = fopen("output.txt", "w");
- //char c;
- if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
- {
- printf("Could not open files");
- exit(1);
- }
- //function to read file 1 and 2, compare it, and output to fp3.
- read_numbers(fp1,fp2,fp3);
- fclose(fp1); fp1 = NULL;
- fclose(fp2); fp2 = NULL;
- fclose(fp3); fp3 = NULL;
- return 0;
- }
- void read_numbers(FILE* fp1, FILE* fp2, FILE* fp3)
- {
- int number1, number2;
- int noc1;
- int noc2;
- int smaller;
- noc1 = fscanf(fp1, "%d", &number1);
- noc2 = fscanf(fp2, "%d", &number2);
- while (noc1 == 1 && noc2 == 1)
- {
- if(number1 <= number2)
- {
- smaller = number1;
- noc1 = fscanf(fp1, "%d", &number1);
- }
- else
- {
- smaller = number2;
- noc2 = fscanf(fp2, "%d", &number2);
- }
- fprintf(fp3, "%d\n",smaller);
- }
- while(noc1 == 1) // If file 1 has stuff
- {
- fprintf(fp3, "%d\n",number1 );
- noc1 = fscanf(fp1, "%d", &number1); // read it and dump it into fp3
- }
- while (noc2 == 1)
- {
- //printf("HI %d\n", number2);
- fprintf(fp3, "%d\n",number2);
- noc2 = fscanf(fp2, "%d", &number2); // read it and dump it into fp3
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement