In: Computer Science
C++
Write a program with the following elements:
in main()
-opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt)
-calls a global function to determine how many lines are in each file
-creates 2 arrays of the proper size
-calls a global function to read the file and populate the array (call this function twice, once for each file/array)
-calls a global 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'
program should check to see if a name is already present in one file or another. If the name is present in both files display the name once in the merged file and add the numbers.
Example:
1st File:
Carlos 7
Tina 3
2nd File:
Lena 2
Carlos 3
Merged File:
Carlos 10
Tina 3
Lena 2
The following will explain you how to open files in C++:
These are a must headers for I/O files programming. #include <fstream> using namespace std;
This is an infut file stream varialbe. ifstream inFile;
To open a file the following syntax must be followed. inFile.open("filename.txt"); //A proper path can also be mentioned.
This is a basic way to check whether the file that is being opened does not exists or is being used by some other user, etc. if (!inFile) { cerr << "Unable to open file filename.txt"; exit(1); // call system to stop }
To read a file we make use of the while loop.
inFile.close();
The following is the brief line of code that allows you to count the number of lines in the file that is opened.
ifstream file("filename.txt");
while (getline(file, l)) //l is a string variable.
count++;
cout << "Numbers of lines in the file : " << count << endl;
The following is a code that explains the opening and closing of files and counting the number of line in the file.
#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { int sum = 0; int x; ifstream inFile; inFile.open("Lab_HW9_2Merge1.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); } Count1(); inFile.close(); inFile.open("Lab_HW9_2Merge1.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); } Count2(); return 0; }
int Count1() { int count = 0;
string line;
ifstream file("Lab_HW9_2Merge1.txt");
while (getline(file, line))
count1++;
cout << "1st File : " << count1 << endl; return 0; } int Count2() { int count = 0;
string line;
ifstream file("Lab_HW9_2Merge2.txt");
while (getline(file, line))
count2++;
cout << "2nd File : " << count2 << endl;
}
This is a way to create arrays in files:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
const int SIZE = 20;
int arrayname[SIZE];
int count;
ifstream inputFile;
inputFile.open("filename.txt");
for(count = 0; count < SIZE; count++);
inputFile >> arrayname[count];
inputFile.close();
cout << "The content is: \n";
for(count = 0; count < SIZE; count++);
cout << arrayname[count] << "";
cout << endl;
system("pause");
return 0;
}