2016-03-18 11 views
4

こんにちは私はシェルを実装するための短いプログラムを書いていますが、私は珍しい問題に遭遇しています。何らかの理由で私はstd :: coutバッファにクリアできません。プログラムはメッセージを印刷しません。簡単な解決策はstd :: cerrに切り替えることですが、coutで印刷するメッセージを取得する方法はありますか?私がしようと試みてきた もの:何が標準出力に書き込まれた後coutバッファをクリアする(C++)

  1. std::cout.flush()
  2. std::endlを挿入します。
  3. 出力ストリームにを挿入します。
  4. std::cout.setf(std::ios::unitbuf);これは出力をバッファリングしないとわかりませんでした。

すべてのヘルプはずっとここに高く評価され、私のコードです:

int main() 
{ 
    //Tryed this to unbuffer cout, no luck. 
    std::cout.setf(std::ios::unitbuf); 

    std::string input; 

    //Print out shell prompt and read in input from keyboard. 
    std::cout << "myshell> "; 
    std::getline(std::cin, input); 

    //********************************************************************** 
    //Step 1) Read in string and parse into tokens. 
    //********************************************************************** 

    char * buf = new char[input.length() + 1]; 
    strcpy(buf, input.c_str()); 

    int index = 0; 
    char * command[256]; 

    command[index] = std::strtok(buf, " "); //Get first token. 
    std::cout << command[index] << std::endl; 

    while (command[index] != NULL) 
    { 
     ++index; 
     command[index] = std::strtok(NULL," "); //Get remaining tokens. 
     std::cout << command[index] << std::endl; 
    } 

    std::cout.flush(); //No luck here either 

    //HERE IS WHERE MY PROBLEM IS. 
    std::cout << index << " items were added to the command array" << std::endl; 

    delete[] buf; 

    return 0; 
} 
+0

あなたはすべてを試したようです_ [ここ](http://stackoverflow.com/questions/36096103/clearing-the-cout-buffer-c)_。投稿した例があなたの実際のコードと一致していることは確かですか?どのような環境(OS、コンパイラ)を使用していますか? – ryyker

+1

この_ [リンク](http://stackoverflow.com/a/13809766/645128)_が役立つ可能性があります。ちなみに、 'cout'は純粋にC++です。あなたの投稿からCタグを削除しました。 – ryyker

+0

私はGeanyをLinux環境でコーディングしています。私はリンクを見てみましょう。私が使用しているコンパイラオプションは以下の通りです: g ++ -Wall -std = C++ 0x -c "%f" ビルドオプション: g ++ -Wall -std = C++ 0x -o "%e" "% f " –

答えて

3

問題は、あなたがUBにつながるwhileループの最後の繰り返しにcoutNULLを送る、としているということですあなたのケースは詰まるcoutです。あなたはcoutに何かを送信する前にNULLをチェックして、あなたは大丈夫です:

if (command[index] != NULL) { 
    std::cout << command[index] << std::endl; 
} 
1

あなたは、あなたのストリームに何が起こったか知っている彼らは、ステータス情報(the iostate, which I recommend you read about)を運ぶことができることを覚えておく必要があります。

try { 
     std::cout.exceptions(std::cout.failbit);   
} catch(const std::ios_base::failure& e) { 
     std::cerr << "stream error: " << e.what() << std::endl; 
     std::cout.clear(); 
} 

// continue working with cout, because std::cout.clear() removed 
// failbit 

あるいは、さらに簡単::次のコードは、あなたのエラーを追跡する助けている可能性が

if(not std::cout) { 
    // address your error (if it is recoverable) 
} 

これはあなたのコードを見ているだろうかあるよう:

#include <cstring> 
#include <string> 
#include <iostream> 
int main() 
{ 
    //Tryed this to unbuffer cout, no luck. 
    std::cout.setf(std::ios::unitbuf); 

    std::string input; 

    //Print out shell prompt and read in input from keyboard. 
    std::cout << "myshell> "; 
    std::getline(std::cin, input); 

    //********************************************************************** 
    //Step 1) Read in string and parse into tokens. 
    //********************************************************************** 

    char * buf = new char[input.length() + 1]; 
    strcpy(buf, input.c_str()); 

    int index = 0; 
    char * command[256]; 

    command[index] = std::strtok(buf, " "); //Get first token. 
    std::cout << command[index] << std::endl; 

    while (command[index] != NULL) 
    { 
     ++index; 
     command[index] = std::strtok(NULL," "); //Get remaining tokens. 
     std::cout << command[index] << std::endl; 
    } 

    // I added from here... 
    if(not std::cout) { 
     std::cerr << "cout is messed up... fixing it..." << std::endl; 
     std::cout.clear(); 
    } 
    // ... to here. 

    std::cout.flush(); //No luck here either 

    //HERE IS WHERE MY PROBLEM IS. 
    std::cout << index << " items were added to the command array" << std::endl; 

    delete[] buf; 

    return 0; 
} 

結果:

$ ./a.out 
myshell> 1 2 3 
1 
2 
3 
cout is messed up... fixing it... 
3 items were added to the command array 
関連する問題