mergng 2 txt file into 1 txt file

hey there. i'm trying to read from 2 sorted txt files and then merge them into 1 sorted txt file. but i can't execute them successfully even though the exe. window is out and then they gave me this error message, yet there is no error found when i compiled.

anyone out there kind enough to help me and point out my mistake? thank you so much!

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>

void readfiles();
void merge();

int main()
{
readfiles();
system ("pause");
return 0;
}

void readfiles()
{
char *first[150], *second[150];
int i =0;
ifstream infile1;

infile1.open("first.TXT", ios::in);

if( infile1.fail())
{
cout<<"Error! The file cannot be opened.\n";
exit(1);
}
else
{
while( !infile1.eof())
{
infile1.get(first[i],20);
infile1.ignore(80,'\n');

i++;


}
}

for (int j = 0; j<=i; j++)
cout<<first[j]<<endl;


ifstream infile2;

infile2.open("second.TXT", ios::in);

if (infile2.fail())
{
cout<<"Error! file cannot be opened.\n";
exit(1);
}

else
{
int j=0;

while( !infile2.eof())
{
infile2.getline(second[j],20);
infile2.ignore(80,'\n');
}
}

int m=0;
int n=0;

ofstream outfile;

outfile.open("new.TXT", ios::out);

if (outfile.fail())
{
cout<<"Error! File could not be created.\n";
exit(1);
}

else
{
while (!infile1.eof() || !infile2.eof())
{
if ( first[m] <second[n] )
{
outfile<<first[m]<<'\n';
m++;
}
else
{
outfile<<second[n]<<'\n';
n++;
}
}
}
outfile.close();
infile1.close();
infile2.close();

}
Look down 3 posts, someone has posted almost exactly the same question. See how they did it, and try to figure it out from that.
that's totally different from mine.
The code might be different but the topic is the same!

Oh my mistake, it's not.

Sorry.

Anyway, then;

You could open the files with fstream, read them into a C string (char* - character pointer) array like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Stores each line in an element within a C-string
    char* myArray1[someNumber];
    std::fstream myFile;
    myFile.open("myTextFile.txt");
    if (myFile.is_open()) {
        int i = 0;
        while (!(myFile.eof())) {
            getline(myFile, myArray1[i]);
            ++i;
    } else {
        std::cout << "Error opening file \"myTextFile.txt\" , check if file exists and can be accessed.\n";
    }

    myFile.close();


Once you've done that, write each array to the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 // Writes each infile to an outfile (thus merging them):
    ofstream myOutFile;
    myOutFile.open("myMergedFile.txt");

    if (myOutFile.is_open()) {
        int i = 0;
        while (!(myFile.eof())) {
            myOutFile << myArray1[i];
            ++i;
        }
    } else {
        std::cout << "Error writing file \"myMergedFile.txt\" , check if previous files could be read and current file can be accessed.\n";
    }

    myOutFile.close();

Then repeat for the second array.
Last edited on
but i can't use his. mine is many lines of char in each file and i have to merge them together to form a sorted txt file
See above. I corrected my mistake, sorry.
erm i have merge the 2 files ans sort them too. anyway, is that for c++?
What do you mean sort them?

And yes, it's C++ but could easily be translated to C; why?
Topic archived. No new replies allowed.