I am getting some errors in this below problem. The problem statement is as follows:
Write a program that reads two input files whose lines are ordered by a key data field. Your program should merge these two files, writing an output file that contains all lines from both files ordered by the key field. As an example, if two input files contain student names and grades for a particular class ordered by name, merge the information as shown below.
File 1:
Adams C
Jones D
King B
File 2:
Barnes A
Johnson C
Output file:
Adams C
Barnes A
Johnson C
Jones D
King B
You must read one line of a file at a time and either write it or the last line read from the other data file to the output file. A common merge algorithm is the following:
Read a line from each data file
While the end of both files has not been reached
If the line from file 1 is smaller than the line from file 2
Write the line from file 2 to the output file and read a new line from file 1
Else
Write the line from file 2 to the output file and read a new line from file 2.
Write the remaining lines (if any) from file 1 to the output file.
Write the remaining lines (if any) from file 2 to the output file.
#include <fstream.h>
#include <iostream.h>
using namespace std;
void merge() {
ifstream ifile1("input1.txt");
ifstream ifile2("input2.txt");
ofstream ofile("output.txt");
std::string temp1;
std::string temp2;
ifile1.getline(temp1, 100);
ifile2.getline(temp2, 100);
while (ifile1 != EOF AND ifile2 != eof) {
while (temp1[i++] != "\n")
;
while (temp2[j++] != "\n")
;
if (i < j) {
ofile << temp2;
ifile1.getline(temp1, 100);
} else {
ofile << temp1;
ifile2.getline(temp2, 100);
}
}
if (ifile1 != eof) {
ifile1.getline(temp1, 100);
ofile << temp1;
}
if (ifile2 != eof) {
ifile2.getline(temp1, 100);
ofile << temp1;
}
}
int main() {
merge();
}
These are the errors I am getting
C:\Users\Ndekwu\Downloads\merge.cpp||In function 'void merge()':|
C:\Users\Ndekwu\Downloads\merge.cpp|5|error: 'ifstream' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|5|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iosfwd|159|note: 'std::ifstream'|
C:\Users\Ndekwu\Downloads\merge.cpp|5|error: expected ';' before 'ifile1'|
C:\Users\Ndekwu\Downloads\merge.cpp|6|error: expected ';' before 'ifile2'|
C:\Users\Ndekwu\Downloads\merge.cpp|7|error: 'ofstream' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|7|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iosfwd|162|note: 'std::ofstream'|
C:\Users\Ndekwu\Downloads\merge.cpp|7|error: expected ';' before 'ofile'|
C:\Users\Ndekwu\Downloads\merge.cpp|11|error: 'ifile1' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|12|error: 'ifile2' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|14|error: 'eof' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|14|error: expected ')' before 'AND'|
Am I using the wrong header file here?