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.
cuong5985Asked:
Who is Participating?
 
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
 
_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 OlivaresSoftware ArchitectCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
 
_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 OlivaresSoftware ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.