LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2006, 10:41 PM   #1
ckoniecny
Member
 
Registered: Oct 2005
Posts: 162

Rep: Reputation: 30
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?
 
Old 09-23-2006, 02:30 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Sounds a bit like a homework assignment ;-)

A couple of tips:

1. Are you sure both files are each in sorted order (if you can assume "sorted order", it definitely simplifies your "merge" algorithm)

2. I think the main loop should probably look more like this:
Code:
  while (!File1.eof()) && !File2.eof())
    ...
    <= I'M NOT SURE YOU NEED TO PUT YOUR READS IN SEPARATE, NESTED LOOPS
3. You might be able to get away with reading a whole line at a time (instead of parsing each line into separate variables), then just doing a simple string compare.

4. The basic algorithm (assuming item "1" above) is to read a line from one file, then keep reading lines (and writing) lines from the second file until the next line is "greater" than the line from the first file. Etc...

'Hope that helps .. at least a little bit ..
 
Old 09-23-2006, 04:02 AM   #3
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
you can do it easily if you know mergesort.
what you are trying to do is 'merging' part in a maergesort.
read about mergesort from any algo book ,
your problem will become trivial.
 
Old 09-23-2006, 09:35 PM   #4
ckoniecny
Member
 
Registered: Oct 2005
Posts: 162

Original Poster
Rep: Reputation: 30
This is what I have so far, but its not working... Whats wrong w/ it??

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

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;
}

Last edited by ckoniecny; 09-24-2006 at 03:53 AM.
 
Old 09-24-2006, 03:29 AM   #5
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
#include <string>
missing at the top.
 
Old 09-26-2006, 09:00 AM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I don't understand your logic in:
Code:
            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;
                }
This performs no sorting, and in fact ALWAYS prints a line from File1, then a line from File2... essentially, just alternating input.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging ,pdf files satimis Linux - General 6 11-21-2007 08:31 PM
merging mpg files karhu Linux - Software 4 07-28-2005 05:25 AM
merging movie files ZaphyR Linux - Software 4 09-05-2004 08:26 AM
merging files using perl pantera Programming 1 06-03-2004 12:56 PM
merging log files help please digitalgravy Linux - Newbie 3 12-10-2003 02:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration