我试图从目录路径的输入中获取两个给定的文本文件,获取它们的整数,然后将这些整数输出到“合并”文本文件中。我是从 Java 开始接触 C++ 的新手,所以到目前为止弄清楚这一点相当棘手。此代码构建并运行,但我实际上并没有获得输出文件。有什么帮助吗?
编辑:
我有合并的文件工作,但我似乎无法让 data1 和 data2 按升序排序,因为它们已实现到输出文件“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();
}