In: Computer Science
IN C LANGUAGE
16.16 Lab 5: merge
Name this program merge.c - This program will take two arguments from the command-line which will be the names of the two text files the program will read from. These text files contain a list of numbers each in ascending order. You'll open the text files and begin merging the two sets of numbers together until every unique number is printed to the screen once and in order. For example:
file1.txt | file2.txt |
---|---|
1 | 2 |
2 | 4 |
3 | 6 |
6 | 7 |
file3.txt: 1 2 5 7 9 10 11 13 15 17 19 20 21 24 25
file4.txt: 3 4 6 8 10 11 12 14 16 18 20
Note: make sure your input text files end with an empty newline, or the last number may be skipped. Lines must end with a newline character, according to the standards we follow.
Example executions:
./a.out file1.txt file2.txt 1 2 3 4 6 7
./a.out file3.txt file4.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 24 25
Here is the solution in pseudo-code:
Read number1 from file1 Read number2 from file2 While ( not EOF for file1 AND not EOF for file2 ) If number1 is less than number2 Print number1 and read the next number from file1 Else if number1 is greater than number2 Print number2 and read the next number from file2 Else (the numbers are the same) Print the number and read the next number from both files End while // at most one of the following two while statements will be true While( file1 has not yet hit EOF) Print number1 and read the next number from file1 While( file2 has not yet hit EOF) Print number2 and read the next number from file2
CODE -
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int number1, number2;
// Opening files for reading
FILE* f1 = fopen(argv[1], "r");
FILE* f2 = fopen(argv[2], "r");
// Reading first number from each file
fscanf(f1, "%d", &number1);
fscanf(f2, "%d", &number2);
// Loop to run till End of file is reached in at least one of the files.
while(!feof(f1) && !feof(f2))
{
// Printing the number from first file if it is smaller and then reading the next number from first file.
if(number1<number2)
{
printf("%d ", number1);
fscanf(f1, "%d", &number1);
}
// Printing the number from second file if it is smaller and then reading the next number from second file.
else if(number1>number2)
{
printf("%d ", number2);
fscanf(f2, "%d", &number2);
}
// Printing the number if both numbers are equal and then reading the next number from both the files.
else
{
printf("%d ", number1);
fscanf(f1, "%d", &number1);
fscanf(f2, "%d", &number2);
}
}
// At most one of the following two while statements will be true
// Printing the rest of the numbers from first file till End of File is reached
while(!feof(f1))
{
printf("%d ", number1);
fscanf(f1, "%d", &number1);
}
// Printing the rest of the numbers from second file till End of File is reached
while(!feof(f2))
{
printf("%d ", number2);
fscanf(f2, "%d", &number2);
}
// Closing the files
fclose(f1);
fclose(f2);
return 0;
}
SCREENSHOTS -
CODE -
INPUT TEXT FILES -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.