Problem with file handling

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::eek: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;
}
 
V

Victor Bazarov

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.

I would use a different approach. Load both files each in its own
vector, sort them, then use 'std::merge' to get the resulting vector,
then dump the vector in the third file.

V
 
J

Jorgen Grahn

If the files are binary, and you call them FOO.TXT, your
users will be angry ...
I would use a different approach. Load both files each in its own
vector, sort them, then use 'std::merge' to get the resulting vector,
then dump the vector in the third file.

That would scale badly to gigabytes of records though. I'm guessing
that's not the intent of the the exercise.

(Not that I find the exercise very useful as it is. I've never seen a
record-based file format like this one where it pays off to seek -- if
you want that kind of thing you use a simple database, or keep your
source files sorted ascending ...)

/Jorgen
 
V

Victor Bazarov

If the files are binary, and you call them FOO.TXT, your
users will be angry ...


That would scale badly to gigabytes of records though. I'm guessing
that's not the intent of the the exercise.

Gigabytes of records? Of *employees*? Really? :)
(Not that I find the exercise very useful as it is. I've never seen a
record-based file format like this one where it pays off to seek -- if
you want that kind of thing you use a simple database, or keep your
source files sorted ascending ...)

V
 
J

Jorgen Grahn

"Employees" could simply be an example.

It *is* clearly an exercise -- which employer keeps just name and ID,
in an ill-defined file format?

A better and more interesting exercise would have taught when to use
Victor's approach (read in everything, process, spit out) and when to
use the pipeline approach (read, process and spit out a few entries at
a time). I like the latter and find that it's underused -- that's why
I complained.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top