B
bintom
I'm having a problem with file handling.
In main(), I have written to 2 binary files -
ABC.TXT, which contains records of type employee in ascending order of
empid and;
XYZ.TXT, which also contains records of type employee, but in
descending order of empid.
I am trying to write all the records in the 2 files, into a 3rd file
NEW.TXT, in ascending order. My program is given below. It writes
correctly to the 1st 2 files but does not do the combined write.
Any help will be appreciated.
Bintom
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
struct employee
{ char name[25];
int empid;
};
void Merge()
{ ifstream f1, f2;
ofstream o;
employee e1, e2;
int i=0, j=0;
f2.open("XYZ.TXT", ios::in|ios::ate|ios::binary);
f2.seekg(0);
while(f2.read((char *)&e2, sizeof(e2)))
j++;
j--;
f2.close();
f1.open("ABC.TXT", ios::in);
f2.open("XYZ.TXT", ios::in);
o.open("NEW.TXT", ios:
ut);
while(1)
{ f1.seekg(i*sizeof(e1), ios::beg);
f2.seekg(j*sizeof(e2), ios::beg);
f1.read((char *)&e1, sizeof(e1));
f2.read((char *)&e2, sizeof(e2));
if(e1.empid < e2.empid)
{ o.write((char *)&e1, sizeof(e1));
++i;
if(!f1) // if EOF of ABC.TXT
break;
}
else
{ o.write((char *)&e2, sizeof(e2));
j--;
if(j < 0) // if BOF of XYZ.TXT
break;
}
}
}
int main()
{ ofstream ofile1("ABC.TXT");
ofstream ofile2("XYZ.TXT");
employee e1 = { "Sid", 18 }, e2 = { "Paul", 19 }, e3 = { "Nickie",
25 };
employee e4 = { "Mel", 23 }, e5 = { "Gina", 20 }, e6 = { "Angela",
17 };
ofile1.write((char *)&e1, sizeof(e1));
ofile1.write((char *)&e2, sizeof(e1)); // Writing to
ABC.TXT
ofile1.write((char *)&e3, sizeof(e1));
ofile2.write((char *)&e4, sizeof(e1));
ofile2.write((char *)&e5, sizeof(e1)); // Writing to
XYZ.TXT
ofile2.write((char *)&e6, sizeof(e1));
Merge();
return 0;
}
In main(), I have written to 2 binary files -
ABC.TXT, which contains records of type employee in ascending order of
empid and;
XYZ.TXT, which also contains records of type employee, but in
descending order of empid.
I am trying to write all the records in the 2 files, into a 3rd file
NEW.TXT, in ascending order. My program is given below. It writes
correctly to the 1st 2 files but does not do the combined write.
Any help will be appreciated.
Bintom
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
struct employee
{ char name[25];
int empid;
};
void Merge()
{ ifstream f1, f2;
ofstream o;
employee e1, e2;
int i=0, j=0;
f2.open("XYZ.TXT", ios::in|ios::ate|ios::binary);
f2.seekg(0);
while(f2.read((char *)&e2, sizeof(e2)))
j++;
j--;
f2.close();
f1.open("ABC.TXT", ios::in);
f2.open("XYZ.TXT", ios::in);
o.open("NEW.TXT", ios:
while(1)
{ f1.seekg(i*sizeof(e1), ios::beg);
f2.seekg(j*sizeof(e2), ios::beg);
f1.read((char *)&e1, sizeof(e1));
f2.read((char *)&e2, sizeof(e2));
if(e1.empid < e2.empid)
{ o.write((char *)&e1, sizeof(e1));
++i;
if(!f1) // if EOF of ABC.TXT
break;
}
else
{ o.write((char *)&e2, sizeof(e2));
j--;
if(j < 0) // if BOF of XYZ.TXT
break;
}
}
}
int main()
{ ofstream ofile1("ABC.TXT");
ofstream ofile2("XYZ.TXT");
employee e1 = { "Sid", 18 }, e2 = { "Paul", 19 }, e3 = { "Nickie",
25 };
employee e4 = { "Mel", 23 }, e5 = { "Gina", 20 }, e6 = { "Angela",
17 };
ofile1.write((char *)&e1, sizeof(e1));
ofile1.write((char *)&e2, sizeof(e1)); // Writing to
ABC.TXT
ofile1.write((char *)&e3, sizeof(e1));
ofile2.write((char *)&e4, sizeof(e1));
ofile2.write((char *)&e5, sizeof(e1)); // Writing to
XYZ.TXT
ofile2.write((char *)&e6, sizeof(e1));
Merge();
return 0;
}