2017-07-11 21 views
1

テキストファイルを読み込んで処理するコードを書きました。自分のPCと別のLinuxシステムで正常に動作していました。しかし、別のLinuxシステムで実行すると、 "ifstream"コマンドで "segmentation fault(core dumped)"というエラーが出ます。私はテキストファイルをチェックし、小さすぎる場合など2本の線は、ファイルがうまく動作しますが、ファイルが大きい場合などに表示されます。 20行の場合、セグメンテーションフォルトエラーでクラッシュします。ifstreamを使用してセグメンテーションフォールトをコアダンプしました

コード原因となるエラーの作品:

int ExtractFragments(int fragmentLength, int overlappingResidues) 
{ 

string line = "", lines = "", interfaceFileName = ""; 

ifstream interfaceList("tempInterfaceList.txt"); 
if (interfaceList) 
{ 

    bool errFlag = false; 

    while (getline(interfaceList, interfaceFileName)) 
    { 
     cout << endl << "interfaces/" << interfaceFileName; 
     ifstream interfaceFile("interface.txt"); //This line crashes 

     if (interfaceFile) 
      cout << "\nHello"; 
    } 
    } 
    return 0; 
} 

このifsreamがセグメンテーションフォールトを引き起こし、どのようにそれを解決するために、なぜ任意のアイデア? ありがとう!

+0

コマンドライン引数はどのように見えますか? – Brandon

+4

[mcve]は良いでしょう... –

+2

デバッガはこれに最適です。 'gdb ' '' <プログラムオプション> ''バックトレース ' – 0x5453

答えて

-1

私は、同じストリームへの同時アクセスがデータ競争を導入する可能性があると考えています。 whileループで入力モードでファイルをオープンしていますが、例外を処理する必要があります。

 int ExtractFragments(int fragmentLength, int overlappingResidues) 
    { 

     ifstream interfaceList("tempInterfaceList.txt"); 
interfaceList.exceptions (std::ifstream::failbit | std::ifstream::badbit); 

try 

    { 

     if (interfaceFile.fail()) 
      { 
      return -1; 
      } 

     bool errFlag = false; 

     while (getline(interfaceList, interfaceFileName)) 
     { 
      cout << endl << "interfaces/" << interfaceFileName; 
      ifstream interfaceFile("interface.txt"); //This line crashes 

      if (interfaceFile) 
       cout << "\nHello"; 
     } 
    } 

    catch(std::ifstream::failure &FileExcep) 
     { 
      cout<<FileExcep.what()<<endl; 
      return -1; 

     } 
     catch(...) 
     { 
      cout<< "other exception thrown"<<endl 
      return -1; 
     } 

     return 0; 

    } 
+0

ありがとう@ロヒニ・シン私はそれをもう一度試みたが、同じエラー。 –

+0

両方のファイルに1000以上の行を置くことに疲れました.allはここで良いです gdbを使用して中核をデバッグする必要があると思われます 例外を使用して効率的な文字列解析とエラー処理を使用する必要があります –

+0

私はdownvotersなぜdownvote plsの理由を説明する –

関連する問題