2017-02-21 3 views
-3

C++ソースファイルを読み込んで、すべての '<'シンボルを「<」に変換し、すべての '>'シンボルを「>」に変換しています。私は主な方法を書いたが、うまくコンパイルされたものはすべてだが、実際にプログラムの先頭にある変換機能を実際に書き出しているので、無限ループに陥り、その犯人が何であるかについて壁を打っている。誰かが私を助けてくれますか? 問題が私のI/Oコーディングにある場合に備えてプログラム全体が含まれていましたが、関数をスラッシュで囲みました。うまくいけば、私は燃え尽きることはありません。ループ内でスタックされたI/Oプログラム

 #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

ここにはバグが多すぎます。信頼性の高い 'while!feof'バグから始まり、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(); 
    } 
関連する問題