【问题标题】:Merging Two Sorted Files with fstream使用 fstream 合并两个已排序的文件
【发布时间】:2013-04-17 00:31:50
【问题描述】:

我正在尝试获取两个已排序的文件,每个文件包含 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

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

【问题讨论】:

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


    【解决方案1】:

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

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2014-10-22
      • 2019-04-29
      • 2016-04-19
      相关资源
      最近更新 更多