class temlate std::basic_ios继承自std::ios_base,定义出与字符类型以及其相应之char_traits相依赖的stream class的共同属性,其中包括stream所用的缓冲器.
构造函数:
- protected:
- basic_ios();
-
- public:
- explicit basic_ios( std::basic_streambuf<CharT,Traits>* sb );
-
-
- public:
- 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:
- #include <iostream>
- #include <iomanip>
- #include <fstream>
-
- int main()
- {
- std::basic_fstream<char> fstream;
- fstream.copyfmt(std::cout);
-
- fstream<< "shihua";
-
- fstream.clear(std::cout.rdstate());
-
- //注意std::basic_fstream使用的buf为std::basic_filebuf.
- //而std::cout使用的buf为std::basic_streambuf.
- //因此我们必须这么调用才能替换掉fstream的buf.
- fstream.std::basic_ios<char>::rdbuf(std::cout.rdbuf());
-
-
- fstream << "woaini";
-
- return 0;
- }
- CharT fill() const;
-
- CharT fill( char ch );
返回用于填充的字符.
设置新的填充字符,并返回设置前填充的字符.
Others:
- std::ios_base::iostate exceptions() const;
-
- void exceptions( std::ios_base::iostate except );
返回当前流的std::ios_base::iostate;
设置当前流的std::ios_base::iostate;
Demo:
- #include <iostream>
- #include <fstream>
-
- int main()
- {
- int ivalue;
- try {
- std::ifstream in("in.txt");
- in.exceptions(std::ifstream::failbit);
- in >> ivalue;
- } catch (std::ios_base::failure &fail) {
- // handle exception here
- }
- }
std::basic_ios::tie
- std::basic_ostream<CharT,Traits>* tie() const;
-
- 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
- std::basic_streambuf<CharT, Traits>* rdbuf() const;
-
- std::basic_streambuf<CharT, Traits>* rdbuf( std::basic_streambuf<CharT, Traits>* sb );
1,返回一个指针指向当前Stream安装的缓冲区.
2,给当前Stream安装一个新的缓冲区,并返回一个指针指向之前的缓冲区.
Demo:
- #include <iostream>
- #include <sstream>
-
- int main()
- {
- std::ostringstream local;
- auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer
-
- std::cout.rdbuf(local.rdbuf()); // substitute internal std::cout buffer with
- // buffer of 'local' object
-
- // now std::cout work with 'local' buffer
- // you don't see this message
- std::cout << "some message";
-
- // go back to old buffer
- std::cout.rdbuf(cout_buff);
-
- // you will see this message
- std::cout << "back to default buffer\n";
-
- // print 'local' content
- std::cout << "local content: " << local.str() << "\n";
- }
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 毁灭性错误,造成不确定状态.