Team LiB
Previous Section Next Section

Chapter 8. The IO Library

第八章 标准 IO 库


Section 8.1 An Object-Oriented Library


Section 8.2 Condition States


Section 8.3 Managing the Output Buffer


Section 8.4 File Input and Output


Section 8.5 String Streams


Chapter Summary


Defined Terms


In C++, input/output is provided through the library. The library defines a family of types that support IO to and from devices such as files and console windows. Additional types allow strings to act like files, which gives us a way to convert data to and from character forms without also doing IO. Each of these IO types defines how to read and write values of the built-in data types. In addition, class designers generally use the library IO facilities to read and write objects of the classes that they define. Class types are usually read and written using the same operators and conventions that the IO library defines for the built-in types.

C++ 的输入/输出(input/output)由标准库提供。标准库定义了一族类型,支持对文件和控制窗口等设备的读写(IO)。还定义了其他一些类型,使 string 对象能够像文件一样操作,从而使我们无须 IO 就能实现数据与字符之间的转换。这些 IO 类型都定义了如何读写内置数据类型的值。此外,一般来说,类的设计者还可以很方便地使用 IO 标准库设施读写自定义类的对象。类类型通常使用 IO 标准库为内置类型定义的操作符和规则来进行读写。

This chapter introduces the fundamentals of the IO library. Later chapters will cover additional capabilities: Chapter 14 will look at how we can write our own input and output operators; Appendix A will cover ways to control formatting and random access to files.

本章将介绍 IO标准库的基础知识,而更多的内容会在后续章节中介绍:第十四章考虑如何编写自己的输入输出操作符:附录 A 则介绍格式控制以及文件的随机访问。

Our programs have already used many IO library facilities:

前面的程序已经使用了多种 IO 标准库提供的工具:

  • istream (input stream) type, which supports input operations


  • ostream (output stream) type, which provides output operations


  • cin (pronounced see-in) an istream object that reads the standard input.

    cin(发音为 see-in):读入标准输入的 istream 对象。

  • cout (pronounced see-out) an ostream object that writes to the standard output

    cout(发音为 see-out):写到标准输出的 ostream 对象。

  • cerr (pronounced see-err) an ostream object that writes to the standard error. cerr is usually used for program error messages.

    cerr(发音为 see-err):输出标准错误的 ostream 对象。cerr 常用于程序错误信息。

  • operator >>, which is used to read input from an istream object

    >> 操作符,用于从 istream 对象中读入输入。

  • operator <<, which is used to write output to an ostream object

    << 操作符,用于把输出写到 ostream 对象中。

  • getline function, which takes a reference to an istream and a reference to a string and reads a word from the istream into the string

    getline 函数,需要分别取 istream 类型和 string 类型的两个引用形参,其功能是从 istream 对象读取一个单词,然后写入 string 对象中。

This chapter looks briefly at some additional IO operations, and discusses support for reading and writing files and strings. Appendix A covers how to control formatting of IO operations, support for random access to files, and support for unformatted IO. This primer does not describe the entire iostream libraryin particular, we do not cover the system-specific implementation details, nor do we discuss the mechanisms by which the library manages input and output buffers or how we might write our own buffer classes. These topics are beyond the scope of this book. Instead, we'll focus on those portions of the IO library that are most useful in ordinary programs.

本章简要地介绍一些附加的 IO 操作,并讨论文件对象和 string 对象的读写。附录 A 会介绍如何控制 IO 操作的格式、文件的随机访问以及无格式的 IO。本书是初级读本,因此不会详细讨论完整的 iostream 标准库——特别是,我们不但没有涉及系统特定的实现细则,也不讨论标准库管理输入输出缓冲区的机制,以及如何编写自定义的缓冲区类。这些话题已超出了本书的范畴。相对而言,本书把重点放在 IO 标准库对普通程序最有用的部分。

Team LiB
Previous Section Next Section