2017-02-21 21 views
-3

我正在研究讀取C++源文件並將所有'<'符號轉換爲「<」並將所有'>'符號轉換爲「>」的代碼。我寫出了主要方法和編譯好的所有東西,但現在我實際上在程序的頂部寫出了我的轉換函數,我陷入了一個無限循環,而且我正在觸發一個關於肇事者的牆。有人可以幫我嗎? 我包含了整個程序,以防問題出在我的I/O編碼上,但我用斜槓包圍了函數。希望我不會被燒傷。卡在循環中的I/O程序C++

 #include <iostream> 
     #include <fstream> 
     #include <cstdlib> 
     #include <string> 
     #include <cstring> 
     using namespace std; 

//FUNCTION GOES THROUGH EACH CHARACTER OF FILE 
//AND CONVERTS ALL < & > TO &lt; or &gt; RESPECTIVELY 

//////////////THIS IS THE FUNCTION IN QUESTION////////// 
void convert (ifstream& inStream, ofstream& outStream){ 
    cout << "start" << endl; 
    char x; 
    inStream.get(x); 
    while (!inStream.eof()){ 
     if (x == '<') 
      outStream << "&lt;"; 
     else if (x == '>') 
      outStream << "&gt;"; 
     else 
      outStream << x; 
    } 
    cout << "end" << endl; 
}; 
/////////////////////////////////////////////////////////////////////////// 


int main(){ 

    //FILE OBJECTS 
    ifstream inputStream; 
    ofstream outputStream; 
    string fileName; 
    //string outFile; 

    //USER PROMPT FOR NAME OF FILE 
    cout << "Please enter the name of the file to be converted: " << endl; 
    cin >> fileName; 
    //outFile = fileName + ".html"; 

    //ASSOCIATES FILE OBJECTS WITH FILES 
    inputStream.open(fileName.c_str()); 
    outputStream.open(fileName + ".html"); 

    //CREATES A CONVERTED OUTPUT WITH <PRE> AT START AND </PRE> AT END 
    outputStream << " <PRE>" << endl; 
    convert(inputStream, outputStream); 
    outputStream << " </PRE>" << endl; 

    inputStream.close(); 
    outputStream.close(); 

    cout << "Conversion complete." << endl; 

    return 0; 
} 
+2

解決這些問題的正確工具是你的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+2

這裏有太多的錯誤。從ol-reliable「while!feof」bug開始,並以一個Loop Of Mystery開頭,神奇地期望一些變量改變它的值,無論如何,在循環的某個地方... –

+2

難以置信地難以達到目的在從不從文件中讀取的循環中的文件。 – user4581301

回答

0

在閱讀文件時,處理文件並不是一種好方法。正確的方法是,首先read整個文件,store的數據,manipulate的存儲數據,然後update的文件。希望這段代碼能幫助你:)

void convert() 
    { 
     int countLines = 0; // To count total lines in file 
     string *lines; // To store all lines 
     string temp; 
     ifstream in; 
     ofstream out; 
     // Opening file to count Lines 
     in.open("filename.txt"); 
     while (!in.eof()) 
     { 
      getline(in, temp); 
      countLines++; 
     } 
     in.close(); 
     // Allocating Memory 
     lines = new string[countLines]; 
     // Open it again to stroe data 
     in.open("filename.txt"); 
     int i = 0; 
     while (!in.eof()) 
     { 
      getline(in, lines[i]); 

      // To check if there is '<' symbol in the following line 
      for (int j = 0; lines[i][j] != '\0'; j++) 
      { 
       // Checking the conditon 
       if (lines[i][j] == '<') 
        lines[i][j] = '>'; 
      } 
      i++; 
     } 
     in.close(); 
     // Now mainuplating the file 
     out.open("filename.txt"); 
     for (int i = 0; i < countLines; i++) 
     { 
      out << lines[i]; 
      if (i < countLines - 1) 
       out << endl; 
     } 
     out.close(); 
    }