Cpp-standard-library-cpp-stringbuf-sputc

提供:Dev Guides
移動先:案内検索

C ++ streambuf-sputc

説明

現在のput位置に文字を格納し、putポインタを増やすために使用され、文字cは制御された出力シーケンスの現在の位置に格納され、位置インジケータを次の文字に進めます。

宣言

以下は、std
streambuf :: sputcの宣言です。
int sputc (char c);

パラメーター

*c* -配置する文字。

戻り値

連続した場合に戻り、int型の値として、文字putが返されます。 それ以外の場合は、ファイル終了値(EOF)を返し、失敗を通知します。

例外

基本的な保証-例外がスローされた場合、ストリームバッファは有効な状態です。

データの競合

ストリームバッファオブジェクトを変更します。

以下の例では、std
streambuf :: sputcについて説明しています。
#include <iostream>
#include <fstream>

int main () {
   char ch;
   std::ofstream ostr ("test.txt");
   if (ostr) {
      std::cout << "Writing to file. Type a dot (.) to end.\n";
      std::streambuf * pbuf = ostr.rdbuf();
      do {
         ch = std::cin.get();
         pbuf->sputc(ch);
      } while (ch!='.');
      ostr.close();
   }
   return 0;
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

Writing to file. Type a dot (.) to end.