C ++ Primer: Chapter 8 summary
Chapter 8: IO Libraries
8.1 IO class
IO libraries and header files types
head File | Types of |
---|---|
iostream | istream, wistream data stream read from ostream, wostream data is written to the stream iostream, wiostream stream read |
fstream | ifstream, wifstream data read from a file ofstream, wofstream data is written to the file fstream, wfstream read and write files |
sstream | istringstream, wistringstream data string read from ostringstream, wostringstream data is written to the string the stringstream, write string wstringstream |
- iostream defines the basic types stream for reading and writing; the fstream defines the type of read and write file name; sstream defines the type of read-write memory string object.
- and istringstream ifstream inherits from istream, ofstream and ostringstream inherited from ostream.
IO object can not copy assignment
- Since the object can not be copied and assigned IO, parameters, or it can not be a type of return flow type. IO operations function normally passed by reference and return flow.
- A write IO object changes its state, it is not transmitted, and the returned reference is const.
Condition Status
Condition Status | description |
---|---|
:: steep iostate | IO is strm type iostate is related to a type of machine, there is provided a fully functional state condition expression |
:: steep badbit | It used to indicate flow has crashed |
:: steep failbit | It used to indicate that an IO operation has failed |
:: steep eofbit | Used to indicate the flow reaches the end of the file |
:: steep goodbit | Used to indicate the flow is not in an error state. This value is zero guarantee |
s.eof() | If the set flow eofbit s Returns true |
s.fail() | If the set flow badbit or failbit s Returns true |
s.bad() | If the set flow badbit s Returns true |
s.good() | If the stream is active s, return true |
s.clear() | S for all the flow condition state bit is reset, the state is set to a valid flow. Return void |
s.clear(flags) | flags strm :: iostate type is according to the given flag bit flags, the flow condition state s corresponding bit is reset. Return void |
s.setstate(flags) | flags strm :: iostate type is according to the given flag bit flags, the flow condition corresponding status bit s. Return void |
s.rdsate () | Returns the current state of the stream s condition, the return type is strm :: iostate |
- Before using a stream, it must first check whether the flow is in good condition.
while(cin>>word)
// ok:读操作成功....
- badbit a system-level error such as an unrecoverable read error, can not be reused in the event stream; represents the failbit recoverable error, as desired read data is inconsistent with the actual read data type, can be corrected after the occurrence of flow can still be used; if reaches the end of file position, and the failbit eofbit will be set; goodbit value of 0, a flow error did not occur.
- Good () Returns all error bits in the case where no set true; Returns true bad (), fail (), eof () at the position corresponding to the error bit is asserted; badbit when the position is, fail () Returns ture. Therefore, use Good () or fail () determine the overall state of the stream.
Output Buffer Management
- Data output stream may be output immediately, and might temporarily stored in the buffer to re-output.
- Flushes of reasons: the program ends normally, as part of the main function of the return operation, the refresh buffer; buffer is full, the subsequent data must first refresh buffer to the write buffer; endl The manipulator using the explicit refreshing buffer; using unitbuf to set the internal state of the stream buffer is empty, is set for unitbuf cerr, refresh may be implemented immediately cerr; output stream cin, cerr cout is associated to another stream, read or write cin cout cerr cause refresh buffer.
- Wrap endl and flush the buffer; flush only refresh buffer; ends first insert a blank character and then flushes the buffer.
- unitbuf flush after each output operation is achieved by the buffer is flushed; nounitbuf to reset the stream, so that the buffer or normal mode.
- If the program crashes, the output buffer is not refreshed its data output may remain in the buffer waiting to be printed.
- tie no arguments returns a pointer to the output stream; ostream accepts a pointer to associate themselves to this tie ostream.
8.2 File Input Output
fstream specific operation
operating | description |
---|---|
fstream fstrm; | fstream fstream header file is a type defined to create an unbound stream file |
fstream fstrm(s); | s is a pointer to a string type or C-style string to create a fstream, and open the file named s of this constructor is explicit |
fstream fstrm(s, mode); | mode included in, out, app, ate, trunc, binary create a fstream, open the file named s specified mode of this constructor is explicit |
fstrm.open(s); | Open the file named s, and will file with the fstrm binding, return void |
fstrm.close(); | The file is closed and fstrm bound to return void |
fstrm.is_open(); | Determine whether the file is opened with fstrm bound to succeed, and has not been closed, return bool |
- 头文件fstream定义3个类型来支持文件IO:ifstream从一个给定文件读取数据;ofstream向一个给定文件写入数据;fstream可以读写给定文件。
使用文件流对象
- C++11标准中,文件名可以是库类型string对象,也可以是C风格字符数组。旧版本只允许C风格字符数组。
- 调用open()失败时,failbit被置位。故最好检测open()是否打开成功。
if(out)
//检测成功后,再使用out
- 若文件流已打开过文件,它调用open()会失败,并使failbit被置位。若文件流想打开其它文件,必须先关闭再打开。
- 当一个fstream对象离开其作用域时,与之关联的文件会自动关闭。
文件模式
文件模式 | 描述 | 前置要求 | 适合对象 |
---|---|---|---|
in | 以读方式打开 | 无 | ifstream、fstream |
out | 以写方式打开 | 默认trunc | ofstream、fstream |
app | 文件末尾进行写操作 | 默认out,无trunc | ofstream、fstream |
trunc | 截断文件,清空文件后再进行写操作 | 有out | ofstream、fstream |
ate | 打开文件后立即定位到文件末尾 | 无 | 任何对象 |
binary | 以二进制方式进行IO | 无 | 任何对象 |
- 单独以out模式打开文件,会默认使用trunc模式,删除旧内容。
- 以out+app模式或者out+in模式打开文件,可保留文件旧内容。
- ifstream默认以in模式打开;ofstream默认以out模式打开; fstream默认以in+out模式打开。
8.3 string流
stringstream特有操作
操作 | 描述 |
---|---|
sstream strm; | sstream是头文件sstream中定义的一个类型 strm是一个未绑定的stringstream对象 |
sstream strm(s); | strm是一个sstream对象,保存string s的一个拷贝。 此构造函数是explicit。 |
strm.str(); | 返回strm所保存的string的拷贝 |
strm.str(s); | 将string s拷贝到strm中,返回void |
- sstream头文件定义3个类型来支持IO:istringtream从string读取数据;ostringtream向string写入数据;stringstream对string读写数据。
- 整行文本处理时,可用istringtream处理行内单词;逐步构造,同一打印输出时使用ostringtream。