• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 783
  • Last Modified:

Write two files to the third file?

How can i write a program that merges the numbers in two files and writes all the numbers into a thrid file. and sorted order from the smallest to the largest in third file.

Start right here
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
using namespace std;


int main( )
{
    ifstream fin;
    ofstream fout;
   


    fin.open("infile_pp13.txt");
    if (fin.fail( ))
    {
        cout <<"Input file opening failed.\n";
        getch();
        exit(1);
    }
   
    fout.open("outfile_pp13.txt");
    if (fout.fail( ))
    {
        cout <<"Output file opening failed.\n";
        getch();
        exit(1);
    }
   
   
    fin.close( );
    fout.close( );

    system("PAUSE");
    return 0;
}

This confuses me, can someone give an idea. Thanks for all your help.
0
cuong5985
Asked:
cuong5985
  • 3
  • 2
  • 2
  • +1
1 Solution
 
_corey_Commented:
Two files have a list of numbers in them.

File 1:

1
2
3
6
8
9

File 2:

2
4
5
6
7
8
9

In the third file, you need all the number from both File 1 and File 2, but they need to be in orer and not just appended:

File 3:
1
2
2
3
4
5
6
6
7
8
8
9
9

corey
0
 
Jaime OlivaresCommented:
Sounds like a homework, here are some steps:
1) try to print all numbers in files, use fin >> somevariable   to read numbers, display them with  cout << somevariable
2) create a vector<int> and use push_back() to insert items into vector instead of displaying
3) make your sort routine by scanning the vector object
0
 
cuong5985Author Commented:
i just wonder what should i do in the program, i know how to copy one file to another, but dont know how to merge them into 3rd file.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
_corey_Commented:
Read one file at a time.  Reading in an integer until end of file.  Add each of these numbers from both files to the *same* vector< int > variable.

After reading in both files, call the sort() method of the variable which is a vector< int > .  The default sort should do a less-than comparison and work for you.

Then loop through each integer in the vector, and output those numbers to the 3rd file.

corey
0
 
B1-66ERCommented:
I think that using "vector" container not good solution, in my opinion will be better use "list" container.
Here some example of code, with comments:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <list> // we will using list container for saving integers from files, bcz this container haz such member functions like sort() and merge()
#include <conio.h>
using namespace std;

int main( )
{
    ifstream fin;
    ofstream fout;
    list<int> l1 ; // for 1.txt
    list<int> l2 ; // for 2.txt
    list<int> l3 ; // for 3.txt
    int i ;
    list<int>::iterator itr ;

   // read all integers from files 1.txt and 2.txt to the containers l1,l2
    fin.open("1.txt");
    if (fin.fail( ))
    {
        cout <<"Input file opening failed.\n";
        getch();
        exit(1);
    }
    while (fin >> i)
     l1.push_back(i) ;
    fin.close() ;

    fin.open("2.txt");
    if (fin.fail( ))
    {
        cout <<"Input file opening failed.\n";
        getch();
        exit(1);
    }

    while (fin >> i)
     l2.push_back(i) ;
    fin.close() ;

   // we sort all elements in containers
    l2.sort() ;
    l1.sort() ;

   // and merge l1 with l2 and write to the l3
    l3 = l1 ;
    l3.merge(l2) ;

   // now saving l3 to 3.txt
 
    fout.open("3.txt");
    if (fin.fail( ))
    {
        cout <<"Output file opening failed.\n";
        getch();
        exit(1);
    }

    for (itr = l3.begin(); itr != l3.end();itr++)
     fout << *itr << '\n' ;

  fout.close() ;

if you want that in 3.txt file waz only unique integers, you can use STL algoritm unique (which move all unique elements in the head of the container) or unique_copy(), for list variable "l3".
0
 
_corey_Commented:
Is it list with the sort method?  Then yes, list would work great.

corey
0
 
cuong5985Author Commented:
but it said that input file opening failed.
0
 
Jaime OlivaresCommented:
where are you running your app from? Maybe some problem with your default directory, try to open Windows Explorer and double click .exe file directly
0
 
B1-66ERCommented:
then try to insert in lines:
 
 ...
 fin.open("1.txt");
 ...
 fin.open("2.txt");
 ...

instead of filenames, full path to this files for ex:
if you using linux OS:

 ...
 fin.open("/path/to/file/1.txt");
 ...
 fin.open("/path/to/file/2.txt");
 ...

if you using Windows OS:

 ...
 fin.open("C:\\path\\to\\file\\1.txt");
 ...
 fin.open("C:\\path\\to\\file\\2.txt"); // you must use '\\' insted of one '\'C:\\path\\to\\file\\
 ...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now