こんにちは私はシェルを実装するための短いプログラムを書いていますが、私は珍しい問題に遭遇しています。何らかの理由で私はstd :: coutバッファにクリアできません。プログラムはメッセージを印刷しません。簡単な解決策はstd :: cerrに切り替えることですが、coutで印刷するメッセージを取得する方法はありますか?私がしようと試みてきた もの:何が標準出力に書き込まれた後coutバッファをクリアする(C++)
std::cout.flush()
- は
std::endl
を挿入します。 - 出力ストリームにを挿入します。
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;
}
あなたはすべてを試したようです_ [ここ](http://stackoverflow.com/questions/36096103/clearing-the-cout-buffer-c)_。投稿した例があなたの実際のコードと一致していることは確かですか?どのような環境(OS、コンパイラ)を使用していますか? – ryyker
この_ [リンク](http://stackoverflow.com/a/13809766/645128)_が役立つ可能性があります。ちなみに、 'cout'は純粋にC++です。あなたの投稿からCタグを削除しました。 – ryyker
私はGeanyをLinux環境でコーディングしています。私はリンクを見てみましょう。私が使用しているコンパイラオプションは以下の通りです: g ++ -Wall -std = C++ 0x -c "%f" ビルドオプション: g ++ -Wall -std = C++ 0x -o "%e" "% f " –