Create a C++ program that: - in main - opens the 2 files provided for input - ca
ID: 3808177 • Letter: C
Question
Create a C++ program that:
- in main
- opens the 2 files provided for input
- calls a function to determine how many lines are in each file
- creates 2 arrays of the proper size
- calls a function to read the file and populate the array (call this
function twice, once for each file/array)
- calls a function to write out the 'merged' results of the 2 arrays
- if there are multiple entries for a person, these should be
merged and should appear as a single entry in the resulting file
- resulting file should be named 'merged_output.txt'
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<limits>
using namespace std;
int file_line(std::ifstream& myfile)
{
int count = 0;
std::string line;
while(getline(myfile,line))
count++;
return count;
}
void read_file(std::ifstream& myfile, int *arr)
{
int test,i=0;
while(myfile >> test)
{
cout<<test<<endl;
arr[i] = test;
i++;
myfile.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
}
}
int main()
{
int count_line1,count_line2,final_size=0;
std::ifstream myfile1("input1.txt");
std::ifstream myfile2("input2.txt");
int *line_f1,*line_f2,*final_arr;
count_line1 = file_line(myfile1);
//cout<<count_line<<endl;
line_f1 = new int(count_line1);
final_size += count_line1;
count_line2 = file_line(myfile2);
line_f2 = new int(count_line2);
final_size += count_line2;
read_file(myfile1,line_f1);
read_file(myfile2,line_f2);
for(int i = 0; i < count_line1; i++)
//cout<<line_f1[i]<<endl;
final_arr = new int(final_size);
for(int i = 0; i < count_line1; i++)
final_arr[i] = line_f1[i];
//for(int i = count_line1; i < final_size; i++)
// final_arr[i] = line_f2[i - count_line1];
for(int i = 0; i < final_size; i++)
//cout<<final_arr[i]<<endl;
return 0;
}