当前位置:   article > 正文

I/O: std::basic_ios

io std

class temlate std::basic_ios继承自std::ios_base,定义出与字符类型以及其相应之char_traits相依赖的stream class的共同属性,其中包括stream所用的缓冲器.

构造函数:

  1. protected:
  2. basic_ios();
  3. public:
  4. explicit basic_ios( std::basic_streambuf<CharT,Traits>* sb );
  5. public:
  6. basic_ios(const basic_ios& ) = delete;

默认构造函数: 是protected的意味着只有其派生类才能调用.

显式构造函数: 是public explicit的接受一个std::basic_streambuf作为参数.

拷贝构造函数: 为delete的因此是不支持拷贝的.

 

State member functions

bool good() const;
	

返回true表示std::ios::goodbit被设置.

如果该流最近的一次I/O操作成功的完成了,那么返回true,如果该流的states被设置了除std::ios::goodbit以外的其他states那么也会返回false.

 

bool eof() const;

返回true表示std::ios::endbit被设置.

如果流在读取数据的过程中到达了文件的尾部(end of file),那么该流的states就会被设置为std::ios::eof,同时该函数会返回true.

 

bool fail() const;
	

返回true表示std::ios::badbit或者std::ios::failbit被设置.

如果流在读取数据的过程中发生了错误,那么std::ios::badbit或者std::ios::failbit被设置,该函数都会返回true.

 

bool bad() const;
	

返回true,表示std::ios::badbit被设置,发生了毁灭性的错误.

 

std::ios::iostate rdstate() const;

返回当前stream已经设置的所有state.

 

void clear( std::ios_base::iostate s = std::ios_base::goodbit );
	

清除当前stream的已有state,并设置s作为作为当前流的state.

 

void setstate( iostate state );
	

给当前流加设state.

 

Format member functions

basic_ios& copyfmt(const basic_ios& other);
	

拷贝other中除了states和streambuf之外的其他任何flag(s),locale等等...

Demo:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. int main()
  5. {
  6. std::basic_fstream<char> fstream;
  7. fstream.copyfmt(std::cout);
  8. fstream<< "shihua";
  9. fstream.clear(std::cout.rdstate());
  10. //注意std::basic_fstream使用的buf为std::basic_filebuf.
  11. //而std::cout使用的buf为std::basic_streambuf.
  12. //因此我们必须这么调用才能替换掉fstream的buf.
  13. fstream.std::basic_ios<char>::rdbuf(std::cout.rdbuf());
  14. fstream << "woaini";
  15. return 0;
  16. }

 

  1. CharT fill() const;
  2. CharT fill( char ch );

返回用于填充的字符.

设置新的填充字符,并返回设置前填充的字符.

 

Others:

  1. std::ios_base::iostate exceptions() const;
  2. void exceptions( std::ios_base::iostate except );

返回当前流的std::ios_base::iostate;

设置当前流的std::ios_base::iostate;

Demo:

  1. #include <iostream>
  2. #include <fstream>
  3. int main()
  4. {
  5. int ivalue;
  6. try {
  7. std::ifstream in("in.txt");
  8. in.exceptions(std::ifstream::failbit);
  9. in >> ivalue;
  10. } catch (std::ios_base::failure &fail) {
  11. // handle exception here
  12. }
  13. }

 

std::basic_ios::tie

  1. std::basic_ostream<CharT,Traits>* tie() const;
  2. std::basic_ostream<CharT,Traits>* tie( std::basic_ostream<CharT,Traits>* str );

1,通过tie()我们可以得到绑定到当前Stream上的一个 std::basic_ostream(输出流),

2,或者给当前Stream绑定一个std::basic_ostream.但是需要注意的是如果当前Stream绑定了一个std::basic_stream,那么每次在调用该Stream的时候都会调用std::basic_ostream的flush()来清空std::basic_ostream的缓冲区.

 

std::basic_ios::rdbuf

  1. std::basic_streambuf<CharT, Traits>* rdbuf() const;
  2. std::basic_streambuf<CharT, Traits>* rdbuf( std::basic_streambuf<CharT, Traits>* sb );

1,返回一个指针指向当前Stream安装的缓冲区.

2,给当前Stream安装一个新的缓冲区,并返回一个指针指向之前的缓冲区.

Demo:

  1. #include <iostream>
  2. #include <sstream>
  3. int main()
  4. {
  5. std::ostringstream local;
  6. auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer
  7. std::cout.rdbuf(local.rdbuf()); // substitute internal std::cout buffer with
  8. // buffer of 'local' object
  9. // now std::cout work with 'local' buffer
  10. // you don't see this message
  11. std::cout << "some message";
  12. // go back to old buffer
  13. std::cout.rdbuf(cout_buff);
  14. // you will see this message
  15. std::cout << "back to default buffer\n";
  16. // print 'local' content
  17. std::cout << "local content: " << local.str() << "\n";
  18. }

 

std::basic_ios::narrow

char narrow( char_type c, char dfault ) const;
	

将当前Stream字符集中的字符C,转为char类型(如果没有对应的char字符,则返回dfault).

其实是相当于: std::use_facet< std::ctype<char_type> >(getloc()).narrow(c, dfault);

 

std::basic_ios::widen

char_type widen( char c ) const;
	

把char字符c转换为当前Stream字符集中的字符.

 

std::ios_base::state类型:

std::ios_base::goodbit 一切都好,没有任何其他bit被谁知.

std::ios_base::eofbit 遇到end-of-file

std::ios_base::failbit 错误;某个I/O动作未成功.

std::ios_base::badbit 毁灭性错误,造成不确定状态.

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/745512

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/432092
推荐阅读
相关标签
  

闽ICP备14008679号