//namespace
using namespace std;
//define the boolean function that returns true if the files are sorted otherwise false
bool files(char fname1[10], char fname2[10])
{
//objects of ifstream class
ifstream file1, file2;
//declare the variables to compare the integer values
int a,b,c,d;
//open the files
file1.open(fname1);
file2.open(fname2);
//set the variable as true
bool ch1 = true;
//use the loop to iterate
while(file1 >> a >> b && file2 >> c >> d)
{
//if the values are not sorted
if(a>b || c>d)
{
//cout < "both="" arrays="" are="" not="">
ch1 = false;
}
//swap the successor values in the preceding term
a = b;
c = d;
}
//returns true
return ch1;
}
Main.cpp
#include "minfunk1.h"
#include "minfunk2.h"
int main()
{
//Create the objects of ifstream class
ifstream file1, file2;
//Create the object of ofstream class
ofstream file3;
//to store the names of the files
char fname1[100], fname2[100], fname3[100];
int ch;
//Ask the user to enter and read the file names
cout<"enter first="" file="" name="" ::="">"enter>
cin>>fname1;
cout<"\nenter second="" file="" name="" ::="">"\nenter>
cin>>fname2;
cout<"\nenter third="" file="" name="" ::="">"\nenter>
cin>>fname3;
//Open the files
file1.open(fname1);
file2.open(fname2);
file3.open(fname3);
//if the files are sorted
if(check(fname1,fname2))
{
cout < "the="" files="" are="" sorted=""><>
//Store the content of file1 into file3
while(file1.eof()==0)
{
file1>>ch;
file3< "="">
}
//Store the content of file2 in the file3
while(file2.eof()==0)
{
file2>>ch;
file3< "="">
}
//Display the message
cout < "the="" items="" of="" "="">< fname1="">< "="" and="" "="">< fname2="">
" are copied to " < fname3="">< "="" successfully"=""><>
}
//if any or both the arrays are not sorted
else
{
cout < "the="" files="" are="" not="">
}
return 0;
}
Two sorted files (File1.txt: 1 3 4 6 7 8 11) (File2.txt: 2 3 5 6 7 9 10 11 12 13)
The C++ program going to check if the file are sorted and merge them to third file (Mergefile.txt: 1 2 3 3 4 5 6 6 7 7 8 9 10 11 11 12 13)