2015-05-01 23 views
2

我目前有EOF位與我的程序正確設置問題。 該程序的要求指出,我將有兩個文件都有排序整數列表。該程序應該從兩個文件中取一個數字,然後比較這些數字。如果一個小於另一個,則較小的一個將被輸出到另一個文件中,然後從它獲得其原始數字的文件中抽取另一個數字。例如,讓我們說我們有文件1與整數1 3 5 9與整數之間的一條新行代替白色空間和文件2與2 4 6 10. 1應該比較2和那麼1應該被繪製到輸出文件中。 1將被數字3替換。奇怪的EOF行爲C++/VIM與其他IDE

爲了幫助我解決這個問題,我研究了在試圖在最後一個數字繪製之後再次繪製一個數字後,EOF標誌被設置。這在代碼中完美地工作我通過VIM創建了輸入文件。在文件1的最後一個數字被輸入到輸出文件後,EOF位被設置,然後程序從第二個文件中獲取所有數字。但是,如果我在任何IDE或文本編輯器中使用相同數量的字符間新聞空間編寫相同的輸入文件,我會看到一些奇怪的EOF行爲。當我在Sublime Text2或Text Edit中編寫相同的文件時,在繪製最後一個數字時EOF位設置正確。因此,假設我們使用命令inputFile2 >> number2,當此命令繪製到最終數字10時,EOF將被設置爲1.在通過VIM創建的文本文件中,如果我們使用命令inputFile2 >> number2並且最後一個數字爲用10表示,EOF不會被設置爲1,直到我運行命令inputFile2 >> number2。

有沒有人有任何知識,爲什麼這兩個編輯之間甚至會有所不同?我已經提供了下面的代碼。有沒有人知道什麼可能會出錯?謝謝你的幫助。

std::ifstream inputFile; 
std::ifstream inputFile2; 
std::ofstream outputFile; 
bool conditionMet = false; 

/* string variables for user input for files */ 
std::string inputName; 
std::string inputName2; 
std::string outputName; 

/* int variables to hold the input from file */ 

int number1, 
    number2; 


inputFile.open("num1.txt"); 
inputFile2.open("num2.txt"); 
outputFile.open("output.txt); 

inputFile >> number1; 
inputFile2 >> number2; 

/* loop until all of the numbers from files are in */ 
while (conditionMet == false) 
{ 

    /* if the first number is less than or equal to the second number */ 
    /* check if the input file for number one is at the end of file */ 
    if (number1 <= number2) 
    { 

     /* if the end of file has been reached for the input file 1 */ 
     if (inputFile.eof()) 
     { 
      /* put all of the numbers from input file 2 until the end of file for the second file */ 
      while (!inputFile2.eof()) 
      { 

       outputFile << number2 << '\n'; 
       inputFile2 >> number2; 
      } 

      /* end the loop */ 

      conditionMet = true; 

     } 

     /* if the end of the file has not been reached then output number from input file 1 and then take in a new number */ 
     else if (!inputFile.eof()) 
     { 
      std::cout << inputFile.eof() << "file1 eof before" << number1 << std::endl; 
      outputFile << number1 << '\n'; 

      inputFile >> number1; 
      std::cout << inputFile.eof() << "file1 eof after" << number1 << std::endl; 
     } 
    } 

    /* if the first number is greater than the second number */ 

    else if (number1 > number2) 
    { 

     /* check if the input file for number two is at the end of file */ 
     /* if the end of file has been reached for the input file 2 */ 
     if (inputFile2.eof()) 
     { 
      /* put all of the numbers from input file until the end of file for the first file */ 
      while (!inputFile.eof()) 
      { 
       outputFile << number1 << '\n'; 
       inputFile >> number1; 
      } 

      /* end the loop */ 
      conditionMet = true; 

     } 


    /* if the end of the file has not been reached, then output number from input file 2 and then take in a new number */ 

     else if (!inputFile2.eof()) 
     { 
      std::cout << inputFile2.eof() << "file2 eof before" << number2 << std::endl; 
      outputFile << number2 << '\n'; 

      inputFile2 >> number2; 
      std::cout << inputFile2.eof() << "file2 eof after" << number2 << std::endl; 
     } 



    } 



} 

/* close all files */ 
inputFile.close(); 
inputFile2.close(); 
outputFile.close(); 

return 0; 

回答

3

這是因爲VIM(和我的配置emacs太)總是用新行結束的文件。這樣做是因爲VIM是面向行的,而且對於C/C++源代碼來說,這確實是由標準規定的。

VIM對於由較短行組成的可能較長的文件確實非常專業化的事實可以通過嘗試使用由單一行組成的10Mb文件來處理(不這樣做)。