2013-04-17 32 views
0

我想將兩個有5000個整數的已排序文件合併成一個10000個整數整數的文件。我有它的工作,除非程序完成其中一個文件,打印出其他文件的其餘部分。用fstream合併兩個排序的文件

這裏是我的方法來合併兩個文件

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

我解決了這個問題。當文件到達結尾時,我不會輸出文件上的最後一個整數。我一次複製了另一個文件的整數,一次是第一個文件結束,第二個是文件錯誤。下面是我如何解決它:

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