Chapter 8. The IO Library第八章 标准 IO 库CONTENTS
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 标准库提供的工具:
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 标准库对普通程序最有用的部分。 |