Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //void compare_size(int* a, int* b);
- void scan_file(FILE* fp1, FILE* fp2, FILE* fp3);
- int main(int argc, char* argv[]) {
- FILE* fp1;
- FILE* fp2;
- FILE* fp3; // eventually
- //first bookend
- fp1 = fopen("numbers1.txt", "r");//creates numbers1.txt
- fp2 = fopen("numbers2.txt", "r");//creates numbers2.txt
- fp3 = fopen("output.txt", "w");//creates number3.txt
- //checks if file1 opened, if not, exit
- if (fp1 == NULL) {
- printf("Failed to open file 1");
- exit(0);//exits with error
- }
- //checks if file 2 opened
- if (fp2 == NULL) {
- printf("Failed to open file 2");
- exit(0);//exits with error
- }
- if (fp3 == NULL) {
- printf("Failed to open file 3");
- exit(0);
- }
- scan_file(fp1, fp2, fp3);
- //second bookend
- fclose(fp1); fp1 = NULL;
- fclose(fp2); fp2 = NULL;
- fclose(fp3); fp3 = NULL;
- return 0;
- }
- void scan_file(FILE* fp1, FILE* fp2, FILE* fp3) {
- int x, y, smaller,larger;
- int noc1, noc2;
- noc1 = fscanf(fp1, "%d", &x);
- noc2 = fscanf(fp2, "%d", &y);
- do {
- if (noc1 == 1 || noc2 == 1) {
- if (x < y) {
- smaller = x;
- noc1 = fscanf(fp1, "%d", &x);
- // larger = y;
- // noc2 = fscanf(fp2, "%d", &y);
- }
- else {
- smaller = y;
- noc2 = fscanf(fp2, "%d", &y);
- //larger = x;
- // noc1 = fscanf(fp1, "%d", &x);
- }
- fprintf(fp3, "%d\n", smaller);
- // fprintf(fp3, "%d\n", larger );
- while (noc1 == 1) {
- fprintf(fp3, "%d\n", x);
- noc1 = fscanf(fp1, "%d", &x);
- }
- while (noc2 == 1) {
- fprintf(fp3, "%d\n", y);
- noc2 = fscanf(fp2, "%d", &y);
- }
- }
- } while (noc1 == 1 || noc2 == 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement