i need help in this problem in C Write a program that merges two files, each con
ID: 3808027 • Letter: I
Question
i need help in this problem in C
Write a program that merges two files, each containing a list of temperatures in ascending order, into a single file containing a list of temperatures in ascending order. There is exactly one newline character after every temperature in the file. Your program must ask the user to enter the names of two input files and a single output file which will contain the merged contents of the two input files. Please note there may be duplicate temperatures in an input file and your output file. Your program must work for any input files in the format specified. Do not make any assumptions about the number of temperatures in the files. An input file can consist of any number of temperatures, zero or more. THIS IS NOT AN ARRAY PROBLEM. IT IS A FILE PROBLEM. Do not define an array to store the temperatures. You should have a maximum of two temperatures in memory at a time. If the user enters the name of an input file that does not exist, your program should display an appropriate error message and then end elegantly. For example, the following is acceptable: I am providing four sample input files that you can hopefully use. You should also include an empty input file and try merging all combinations of the five files to test your program. Temps1Win.txt, Temps2Win.txt, Temps3Win.txt and Teinps4Win.txt are intended to be readable by PCs. Temps1Mac.txt, Temps2Mac.txt, Temps3Mac.txt and Temps4Mac.txt are intended to be readable by Macs. If you have problems with these files, you may have to create your own. Please note there is a newline character after every temperature including the last temperature. If you copy and paste the contents of these files rather than saving them when opening them in a web browser, you may miss this last character.Explanation / Answer
Here is the code for you:
#include <stdio.h>
#define NAME_LENGTH 50
int main()
{
FILE *fp1, *fp2, *fp3;
char tempFile[NAME_LENGTH];
puts("Enter the name of the first temperature file to be used as input.");
scanf("%s", tempFile);
fp1 = fopen(tempFile, "r");
if(fp1 == NULL)
{
printf("Error: Unable to open %s. ", tempFile);
return 0;
}
puts("Enter the name of the second temperature file to be used as input.");
scanf("%s", tempFile);
fp2 = fopen(tempFile, "r");
if(fp2 == NULL)
{
printf("Error: Unable to open %s. ", tempFile);
return 0;
}
puts("Enter the name of the temperature file to be used as output.");
scanf("%s", tempFile);
fp3 = fopen(tempFile, "w");
float num1;
fscanf(fp1, "%f", &num1);
float num2;
fscanf(fp2, "%f", &num2);
while(!feof(fp1) && !feof(fp2))
{
if(num1 < num2)
{
fprintf(fp3, "%f ", num1);
fscanf(fp1, "%f", &num1);
}
else if(num1 > num2)
{
fprintf(fp3, "%f ", num2);
fscanf(fp2, "%f", &num2);
}
else
fscanf(fp2, "%f", &num2);
}
while(!feof(fp1))
{
fprintf(fp3, "%f ", num1);
fscanf(fp1, "%f", &num1);
}
while(!feof(fp2))
{
fprintf(fp3, "%f ", num2);
fscanf(fp2, "%f", &num2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
printf("creates the file %s that contains ", tempFile);
fp3 = fopen(tempFile, "r");
while(!feof(fp3))
{
fscanf(fp3, "%f", &num1);
printf("%f ", num1);
}
printf(" ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.