Results 1 to 5 of 5

Thread: Merging Two Files using C++

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    33

    Merging Two Files using C++

    Is there a simple way of merging File1 and File2 into OutFile without the use of arrays, only using fstream?

    File1:
    111 Joe Smith
    333 Steve Smith
    555 Ron Smith

    File2:
    222 Don Smith
    444 Randy Smith

    OutFile:
    111 Joe Smith
    222 Randy Smith
    333 Steve Smith
    444 Randy Smith
    555 Ron Smith

  2. #2
    Junior Member
    Join Date
    Sep 2006
    Posts
    28

    Re: Merging Two Files using C++

    Why don't you want to use arrays? (I can't think of any other way)

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    33

    Re: Merging Two Files using C++

    Unfortunately to make my life more difficult I was told not to. This is the code I have so far:

    Code:
    #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;
    }

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Merging Two Files using C++

    Absolutely it's possible. The algorithm is pretty simple. here's a psuedcode example.

    Code:
    // read a line from reach file (file a, file b)
    storedA = a.readline();
    storedB = b.readline();
       
    // loop until one of the files is empty
    while(!a.eof() && !b.eof())
    {
       // if a comes before b, write it, and read the next line from A.
       // loop until this next line doesn't come before line B
       while(storedA comes before storedB)
       {
          write(storedA);
          storedA = a.readline();
       }
       
       // repeat the process for B < A.
       while(storedB comes before storedA)
       {
          write(storedB);
          storedB = b.readline();
       }
       
    }
    
    // here check if either of the files has reached End Of File.
    // if true, write the rest of the other file.
    You probably need to check for EOF inside each of the inner while loops, but I left that out. Good luck!
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Merging Two Files using C++

    Don't use eof() in loop conditions. Directly check the stream objects.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width