C++ I/O 重定向方法(定向到串口或Socket)(二)

2014-11-24 09:00:29 · 作者: · 浏览: 2
tBuf(socket); std::ostream outStream(&outBuf); std::string line; while (std::getline(std::cin, line)) { outStream << line << std::endl; }

上面的代码用于将控制台上的输入写入到套接字中。

用于输入的streambuf

basic_streambuf中输入相关的操作有sgetc,sbumpc,sgetn,sungetc和sputbackc。其中sungetc和sputbackc用于回退字符,这个功能不常用到,而且也不太可能在套接字上回退字符,因此这里省略对回退字符的介绍,关于这方面的内容可以参考《C++标准程序库》。

sgetc和sbumpc都用于读取一个字符,区别是后者会将读取位置向后移动一个位置,而前者不会改变读取位置。如果没有提供缓冲区,或者缓冲区的内容已经读完,那么sgetc会调用underflow方法,而sbumpc会调用uflow方法,从外部设备读取更多数据。uflow的默认行为是调用underflow,然后移动缓冲区的读取指针,如果没有提供缓冲区,则必须同时重写underflow和uflow。sgetn用于读取多个字符,它会调用xsgetn,而xsgetn的默认行为是依次调用sbumpc,如果为了改善读取多个字符的性能,可以重写xsgetn方法。

basic_streambuf的源码中:

  1. int_type
  2. sputc(char_type __c)
  3. {
  4. int_type __ret;
  5. if (__builtin_expect(this->pptr() < this->epptr(), true))
  6. {
  7. *this->pptr() = __c;
  8. this->pbump(1);
  9. __ret = traits_type::to_int_type(__c);
  10. }
  11. else
  12. __ret = this->overflow(traits_type::to_int_type(__c));
  13. return __ret;
  14. }
  15. /**
  16. * @brief Multiple character insertion.
  17. * @param s A buffer area.
  18. * @param n Maximum number of characters to write.
  19. * @return The number of characters written.
  20. *
  21. * Writes @a s[0] through @a s[n-1] to the output sequence, as if
  22. * by @c sputc(). Stops when either @a n characters have been
  23. * copied, or when @c sputc() would return @c traits::eof().
  24. *
  25. * It is expected that derived classes provide a more efficient
  26. * implementation by overriding this definition.
  27. */
  28. virtual streamsize
  29. xsputn(const char_type* __s, streamsize __n);
  30. /**
  31. * @brief Consumes data from the buffer; writes to the
  32. * controlled sequence.
  33. * @param c An additional character to consume.
  34. * @return eof() to indicate failure, something else (usually
  35. * @a c, or not_eof())
  36. *
  37. * Informally, this function is called when the output buffer
  38. * is full (or does not exist, as buffering need not actually
  39. * be done). If a buffer exists, it is @a consumed, with
  40. * some effect on the controlled sequence.
  41. * (Typically, the buffer is written out to the sequence
  42. * verbatim.) In either case, the character @a c is also
  43. * written out, if @a c is not @c eof().
  44. *
  45. * For a formal definition of this function, see a good text
  46. * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
  47. *
  48. * A functioning output streambuf can be created by overriding only
  49. * this function (no buffer area will be used).
  50. *
  51. * @note Base class version does nothing, returns eof().
  52. */
  53. virtual int_type
  54. overflow(int_type /* __c */ = traits_type::eof())
  55. { return traits_type::eof(); }
  56. /**
  57. * @brief Synchronizes the buffer arrays with the controlled sequences.
  58. * @return -1 on failure.
  59. *
  60. * Each derived class provides its own appropriate behavior,
  61. * including the definition of @a failure.
  62. * @note Base class version does nothing, returns zero.
  63. */
  64. virtual int
  65. sync() { return 0; }
  66. /**
  67. * @brief Entry point for all single-character output functions.
  68. * @param s A buffer read area.
  69. * @param n A count.
  70. *
  71. * One of two public output functions.
  72. *
  73. *
  74. * Returns xsputn(s,n). The effect is to write @a s[0] through
  75. * @a s[n-1] to the output sequence, if possible.
  76. */
  77. streamsize
  78. sputn(const char_type* __s, streamsize __n)
  79. { return this->xsputn(__s, __n); }
  80. /**
  81. * @brief Synchronizes the buffer arrays with the controlled sequences.
  82. * @return -1 on failure.
  83. *
  84. * Each derived class provides its own appropriate behavior,
  85. * including the definition of @a failure.
  86. * @note Base class version does nothing, returns zero.
  87. */
  88. virtual int
  89. sync() { return 0; }

    无缓冲方式

    首先来看下无缓冲方式的输入实现,如