使用fstream合并两个排序文件

时间:2013-04-17 00:31:50

标签: visual-c++ merge ifstream getline ofstream

我正在尝试使用每个5000个整数的两个排序文件,并将它们组合成一个10000个排序整数的文件。我有它的工作,除非程序完成其中一个文件,打印出其他文件的其余部分。

这是我合并两个文件的方法

void mergeFiles(string inFile1, string inFile2, string outFile) {
        ifstream fin(inFile1);
        ifstream fin2(inFile2);
        ofstream fout(outFile);

        string line;
        int i = 1;
        int in2 = 0, in1 = 0;
        if(fin) {
            getline(fin,line);
            in1 = atoi(line.c_str());
        }
        if(fin2) {
            getline(fin2,line);
            in2 = atoi(line.c_str());
        }
        bool first = true;
        while(fin || fin2) {
            if(fin && fin2) {
                if(in2 <= in1) {
                    fout << i++ << ": " << in2 << endl;
                    getline(fin2, line);
                    in2 = atoi(line.c_str());
                }
                else {
                    fout << i++ << ": " << in1 << endl;
                    getline(fin, line);
                    in1 = atoi(line.c_str());
                }
            }
            else {
                            // This is the part giving me trouble
                            // Code Snippets below go here
            }
        }
    }

取决于我使用它:

fout << i++ << ": " << line << endl;
if(fin)
     getline(fin, line);
else if(fin2) 
     getline(fin2, line);

我的输出文件的最后5行如下所示:

9996: 99933
9997: 99943
9998: 99947
9999: 99947
10000: 99993

if(fin)
     getline(fin, line);
else if(fin2) 
     getline(fin2, line);
fout << i++ << ": " << line << endl;

我文件的最后5行看起来像这样:

9996: 99933
9997: 99943
9998: 99947
9999: 99993
10000: 99993

我文件的最后5行应该是这样的:

9996: 99933
9997: 99943
9998: 99947
9999: 99957
10000: 99993

我知道它与从文件中抓取下一行和算法的过程有关。关于我如何解决它的任何想法?

1 个答案:

答案 0 :(得分:2)

我解决了这个问题。到达结尾时,我没有输出文件的最后一个整数。我复制了另一个文件的整数两次,一次是在第一个文件结束时,第二次是在文件为假时。以下是我解决它的方法:

    void mergeFiles(string inFile1, string inFile2, string outFile) {
    // Open Files
    ifstream fin(inFile1);
    ifstream fin2(inFile2);
    ofstream fout(outFile);

    string line;                // string to hold line from file
    int i = 1;                  // line counter
    int in2 = 0, in1 = 0;       // ints to hold ints from each file
    if(fin) {                   // if file is open
        getline(fin,line);      // get first line
        in1 = atoi(line.c_str());   // convert to int
    }
    if(fin2) {                  // if file is open
        getline(fin2,line);     // get first line
        in2 = atoi(line.c_str());   // convert to int
    }
    bool first = true;          // bool to catch when a file closes
    while(fin || fin2) {        // if either file is still open
        if(fin && fin2) {       // if both files are still open
            if(in2 <= in1) {    // file 2 has the smaller int
                fout << i++ << ": " << in2 << endl; // print file 2 int to output file
                getline(fin2, line);        // get next line from file 2
                in2 = atoi(line.c_str());   // convert to int
            }
            else {              // file 1 has smaller int
                fout << i++ << ": " << in1 << endl; // print file 1 int to output file
                getline(fin, line);         // get next line from file 1
                in1 = atoi(line.c_str());   // convert to int
            }
        }//endif
        else {      // if one of the files has finished
            if(first) {     // first time through the else
                if(!fin)        fout << i++ << ": " << in2 << endl;     // Depending on which file closed
                else if(!fin2)  fout << i++ << ": " << in1 << endl;     // print the int before printing lines
            }//endif
            else
                fout << i++ << ": " << line << endl;    // don't need to convert just print at this point

            // get the next line from the file that is open
            if(fin)         getline(fin, line); 
            else if(fin2)   getline(fin2, line);

            first = false;  // only print line from now on, don't print in1 or in2
        }// endelse
    }//endwhile
}//endmethod