473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Merging Two Files using C++

I have the following two files:
File1:
11 John Doe
33 Jane Doe
55 Steve Smith

File2:
22 Joe Doe
44 Willy Widget

I'm trying to merge the two files to look like:

Output:
11 John Doe
22 Joe doe
33 Jane Doe
44 Willy Widget
55 Steve Smith

Note: I cannot use array's to sort.

This is the code I have thus far:

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream File1, File2;
string File1_FirstName, File1_LastName;
string File2_FirstName, File2_LastName;
int File1_num, File2_num, NumberOne, NumberTwo;
ofstream outFile;

File1.open("File1.txt", ios::in);
File2.open("File2.txt", ios::in);

outFile.open("Output.txt", ios::out);

while (!File2.eof())
{
File1 >File1_num >File1_FirstName >File1_LastName;
NumberOne = File1_num;

File2 >File2_num >File2_FirstName >File2_LastName;
while (!File1.eof())
{
File1 >File1_num >File1_FirstName >File1_LastName;
NumberTwo = File1_num;

if ( NumberOne File2_num < NumberTwo )
{
outFile << NumberOne << '\t'
<< FirstName << '\t'
<< LastName << '\n';
}
}
}
return 0;
}
My output is not what I expected, I'm having logic issues. Can anyone
point me in the correct direction?

Sep 23 '06 #1
5 5853
In article <11**********************@m7g2000cwm.googlegroups. com>,
ck*******@gmail.com says...
I have the following two files:
File1:
11 John Doe
33 Jane Doe
55 Steve Smith

File2:
22 Joe Doe
44 Willy Widget

I'm trying to merge the two files to look like:

Output:
11 John Doe
22 Joe doe
33 Jane Doe
44 Willy Widget
55 Steve Smith
I'd define a class that holds the data and defines operator< to sort the
data in the desired fashion (you haven't described what should happen
if, for example, the same number appears with two different names).

From there, you can directly merge from input files to an output file if
the input files are guaranteed to be sorted, as you've shown them here.
Otherwise, you can copy from the input files to a sorted container (e.g.
set or multiset, depending on whether duplicate keys are allowed) and
then merge from there to the output.

I'm not sure why can _can't_ use arrays (as you mentioned in a snipped
portion of your post) but I'd certainly consider it ill-advised.

As far as the code you have goes, something like this:
while (!File2.eof())
{
File1 >File1_num >File1_FirstName >File1_LastName;

is essentially certain to be incorrect, and normally needs to be
rewritten to something like:

while (file1>>input1>>input2>>input3>>inputN)
// whatever

The important point is that you need to check for a problem with reading
the input file AS you read it -- file.eof() only becomes true AFTER
you've reached the end of the file, so your loop attempts to read the
last data in the file twice. I'd forget about using explicit loops,
however, and just use an std::istream_iterator to read the input.

Your code also has a problem in that each iteration through the loop
reads input from each file, but only writes an output from one file --
and discards the input it read from the other file. Each iteration
should read only ONE input, to replace the one just written out.

As previously noted, however, I'd use std::merge() for this job -- it's
written specifically for tasks like this, and does them quite nicely.

class record {
int number;
std::string first_name, last_name;
public:
friend
std::istream &operator>>(std::istream &in, record &r) {
return in >r.number >r.first_name >r.last_name;
}

friend std::ostream &operator<<(std::ostream &os, record const &r) {
return os << r.number << "\t"
<< r.first_name << "\t"
<< r.last_name;
}

bool operator<(record const &other) const {
return number < other.number;
}
};
// ...
std::merge(
std::istream_iterator<record>(infile1),
std::istream_iterator<record>(),
std::istream_iterator<record>(infile2),
std::istream_iterator<record>(),
std::ostream_iterator<record>(outfile, "\n"));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 23 '06 #2
Thats a little bit above my level. Is there anyway I can email you
what I have so you can see my actual program?

Jerry Coffin wrote:
In article <11**********************@m7g2000cwm.googlegroups. com>,
ck*******@gmail.com says...
I have the following two files:
File1:
11 John Doe
33 Jane Doe
55 Steve Smith

File2:
22 Joe Doe
44 Willy Widget

I'm trying to merge the two files to look like:

Output:
11 John Doe
22 Joe doe
33 Jane Doe
44 Willy Widget
55 Steve Smith

I'd define a class that holds the data and defines operator< to sort the
data in the desired fashion (you haven't described what should happen
if, for example, the same number appears with two different names).

From there, you can directly merge from input files to an output file if
the input files are guaranteed to be sorted, as you've shown them here.
Otherwise, you can copy from the input files to a sorted container (e.g.
set or multiset, depending on whether duplicate keys are allowed) and
then merge from there to the output.

I'm not sure why can _can't_ use arrays (as you mentioned in a snipped
portion of your post) but I'd certainly consider it ill-advised.

As far as the code you have goes, something like this:
while (!File2.eof())
{
File1 >File1_num >File1_FirstName >File1_LastName;

is essentially certain to be incorrect, and normally needs to be
rewritten to something like:

while (file1>>input1>>input2>>input3>>inputN)
// whatever

The important point is that you need to check for a problem with reading
the input file AS you read it -- file.eof() only becomes true AFTER
you've reached the end of the file, so your loop attempts to read the
last data in the file twice. I'd forget about using explicit loops,
however, and just use an std::istream_iterator to read the input.

Your code also has a problem in that each iteration through the loop
reads input from each file, but only writes an output from one file --
and discards the input it read from the other file. Each iteration
should read only ONE input, to replace the one just written out.

As previously noted, however, I'd use std::merge() for this job -- it's
written specifically for tasks like this, and does them quite nicely.

class record {
int number;
std::string first_name, last_name;
public:
friend
std::istream &operator>>(std::istream &in, record &r) {
return in >r.number >r.first_name >r.last_name;
}

friend std::ostream &operator<<(std::ostream &os, record const &r) {
return os << r.number << "\t"
<< r.first_name << "\t"
<< r.last_name;
}

bool operator<(record const &other) const {
return number < other.number;
}
};
// ...
std::merge(
std::istream_iterator<record>(infile1),
std::istream_iterator<record>(),
std::istream_iterator<record>(infile2),
std::istream_iterator<record>(),
std::ostream_iterator<record>(outfile, "\n"));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 23 '06 #3
In article <11**********************@b28g2000cwb.googlegroups .com>,
ck*******@gmail.com says...
Thats a little bit above my level. Is there anyway I can email you
what I have so you can see my actual program?
I do have email of course, but if you want help, it's generally better
to post here. That way 1) other people can chip in, and 2) other people
can learn from the discussion.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 23 '06 #4
This is what I have so far and its still not working.... Any hints?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream File1;
ifstream File2;
ofstream outFile;

string File1_fName, File1_lName;
string File2_fName, File2_lName;
int File1_acctNum, File2_acctNum;

File1.open("file1", ios::in);
File2.open("file2", ios::in);
outFile.open("out.txt", ios::out);

File1 >File1_acctNum >File1_fName >File1_lName;
File2 >File2_acctNum >File2_fName >File2_lName;
while (!File1.eof() && !File2.eof())
{
if (File2_acctNum File1_acctNum)
{
outFile << File1_acctNum << '\t' << File1_fName
<< '\t'
<< File1_lName << " *\n";
outFile << File2_acctNum << '\t' << File2_fName
<< '\t'
<< File2_lName << " *\n";
File1 >File1_acctNum >File1_fName >>
File1_lName;
File2 >File2_acctNum >File2_fName >>
File2_lName;
cout << File2_acctNum << "\t" << File2_fName <<
"\t" << File2_lName << endl;
}
else if (File1.eof())
{
while (!File2.eof())
{
outFile << File2_acctNum << '\t' <<
File2_fName << '\t'
<< File2_lName << '\n';
}
}
else if (File2.eof())
{
while(!File1.eof())
{
outFile << File1_acctNum << '\t' <<
File1_fName << '\t'
<< File1_lName << '\n';
}
}

}
return 0;
}

Sep 24 '06 #5
In article <11*********************@m7g2000cwm.googlegroups.c om>,
ck*******@gmail.com says...
This is what I have so far and its still not working.... Any hints?
Not any new ones, really.
while (!File1.eof() && !File2.eof())
As already noted, code like this is almost always wrong. You want to
check for EOF _as_ you read the data.

I'd also split things up into a couple of functions, such as
"copy_remainder" (or something like that) to copy the remainder of a
file to the output.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anilza Popat | last post by:
I would like to know how to encrypt and decrypt files using c#. I want a program that asks for password before encrypt or decrypt the file. best regards
3
by: Dan Kjaergaard | last post by:
Hi I created a program using C#, which iterates files and displays these in a listview. However I would like to filter the files, so files won't be displayed if the current user hasn't "read...
2
by: Vanga Sasidhar | last post by:
Already posted the same message but the date and time of my machine was set back. and it was listed under the old date. Thats why I am posting the same message again. Please accept this. ...
11
by: MadMonk | last post by:
Hi, I need to write a small Windows application (console or forms) (for Windows XP SP2 & Windows Server 2003), which will check a folder for the presence of some files, and if they exist, it...
0
by: pbd22 | last post by:
Hi. I am having a really tough time here and would appreciate some help. i am using an iFrame to pass a url string of files to upload to server. on the client i have created a multiple...
0
by: dixonjm | last post by:
Hi, I have a master page & various pages that will use this master page. Each content page will have a CSS & JS file which will be named the same as the content page. When I try to load the CSS...
2
by: karen.google | last post by:
I have an SSIS package that I'm converting from DTS (SQLServer 2005), and the ActiveX Script Task (in VBScript) is deprecated, so I'm trying to convert things to Script tasks (in VB .net). I...
0
by: ravitunk | last post by:
hello all...can anyone tell me how to merge two pdf files using VB6...I have a pdf with plain text and the other pdf with a header and footer....I want to merge these two pdf's....please reply...
0
by: Albert-jan Roskam | last post by:
Hi John, Thanks! Using a higher xlrd version did the trick! Regarding your other remarks: -yep, input files with multiple sheets don't work yet. I kinda repressed that ;-) Spss outputs only...
0
by: brat33 | last post by:
I have lots of files that need monthly cleanup done on them. These files are located in many different directories on the same server. Some example files names are C1234.idx, C1111.idx, C8693.idx,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.