0

I'm trying to take two given text files from input of a directory path, take their integers, and output those integers into a "merged" text file. I'm new to C++ from Java, so this has been rather tricky thus far figuring out. This code builds and runs, but I'm not actually getting an output file. Any help?

Edit:

I have the merged file working, but I can't seem to get the data1 and data2 to sort into ascending order as they're implemented into the output file "merge.txt".

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void textMerge(){
    ifstream inFile1;
    ifstream inFile2;
    string inputFileName1;
    string inputFileName2;
    ofstream outputFile("merge.txt");
    cout << "1. Please enter the directory path of the first text list: " << endl;    // Open first file based on given directory path
    cin >> inputFileName1;
    inFile1.open(inputFileName1);
    cout << "2. Please enter the directory path of the second text list: " << endl;  // Repeat open file for second directory path
    cin >> inputFileName2;
    inFile2.open(inputFileName2);
    if (inFile1.is_open() && inFile2.is_open()){
    int data1;
    int data2;
    while(!inFile1.eof() && !inFile2.eof()){                // while both files haven't reached end-of-file yet
        inFile1 >> data1;        // get next number for first file
        inFile2 >> data2;
                                 // get next number for second file
        outputFile << data1 << data2;     // output each integer from files 1 and 2 to outputFile(merge.txt)
    }
    inFile1.close();
    inFile2.close();
    outputFile.close();
    } 
    else{
        cerr << "The file(s) failed to open properly.";
        exit(0);
    }
}
int main(){
    textMerge();
}
blunatic
  • 309
  • 1
  • 5
  • 16
  • 2
    [Don't use `eof()` inside a loop condition](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Your `while(!inFile1.eof() && !inFile2.eof())` becomes `while(inFile1 >> data1 && inFile2 >> data2)` (and remove the first two statements in the loop body). Also, try adding some debug logging, for example `cout << ofstream.good() << endl;` just after `ofstream outputFile("merge.txt");`, and `cout << data1 << " " << data2 << endl;` just before `outputFile << data1 << data2;`. (Also you could indent your code a bit better...) – gx_ Sep 07 '13 at 19:39
  • Thanks, that helped me out a bunch. I'm still not seeing an actual outputFile being written anywhere though, any ideas on that? (Also the indenting was sloppy because I've been playing around with different ways of approaching this for a few hours. Sorry 'bout that) – blunatic Sep 07 '13 at 20:31

2 Answers2

0

There is nothing wrong with your code. What might be wrong is the file names you are using when running the program. Be sure to provide the full file name (including the path).

0

I wonder why you have included vector header but leaving that, you should give the whole path as input rather than the absolute path. Also change your while condition to

while(inFile1>>data1 && inFile2>>data2)

As this check whether the read into variables was sucess or not rather than whether the file has reached end or not.

Saksham
  • 8,110
  • 6
  • 35
  • 63
  • I mistakenly left in the header because I was playing around with different ways to tackling this earlier. – blunatic Sep 07 '13 at 22:58