---
title: "C++之标准IO库"
author: "rothmada"
date: "2012-08-16T11:41:26Z"
tags: []
categories: ["编程语言"]
slug: "c-io"
draft: false
lastmod: "2012-08-16T11:42:37Z"
id: 1633745
url: "https://rothmada.iteye.com/blog/1633745"
---

C++之标准IO库

 

**一、概述**

C++的输入/输出（input/output）由标准库提供。

多种IO标准库工具：

- istream，提供输入操作
- ostream，提供输出操作
- cin：读入标准输入的istream对象
- cout：写到标准输出的ostream对象
- cerr：输出标准错误的ostream对象。常用语程序错误信息
- &gt;&gt;，用于从istream对象中读入输入
- &lt;&lt;，用于把输出写到ostream对象中
- getline，功能是从istream对象读取一个单词，然后写入string对象中

**二、面向对象的标准库**

![](http://dl.iteye.com/upload/attachment/0072/3948/65205ca1-b37c-3cd0-a903-561202902a95.png)  
 

**1.国际字符的支持**

流类（stream class）读写都是由char类型组成的流。

标准库还定义了一组相关的类型，支持wchar\_t类型。每个类都加上“w”前缀以此区分。

控制窗口读写：wostream,wistream,wiostream

文件输入输出：wofstream,wifstream,wfstream

String输入输出：wostirngstream,wistringstream,wstringstream

对象：wcin,wcout,wcerr

 

**2.IO对象不可复制或赋值**

流对象不能存储在vector或其他容器中。

形参或返回类型也不能为流类型，如果需要，则必须传递或返回指向该对象的指针或引用

```
if(cin)
	//ok to use cin, it is in a valid state
while(cin >> word)
	//ok:read operation successful…
```

流的状态由bad,fail,eof,good操作揭示。

bad:系统级的故障

fail:可恢复的错误

eof:遇到文件结束符，通常还会设置fail

clear:重设为有效条件

setstate：打开某个指定的条件，用于表示某个问题的发

 

**1.条件状态的访问**

rdstate成员函数返回一个iostate类型的值，该值对于于流当前的整个条件状态

```
istream::iostate old_state = cin.rdstate();
```

**2.多种状态的处理**

 

```
is.setstate(ifstream::badbit | ifstream::failbit);
```

**三、输出缓冲区的管理**

**1.输出缓冲区的刷新**

```
cout << “hi” << flush;  //flushes the buffer;add no data
cout << “hi” << ends;  //inserts a null,then flushes the buffer
cout << “hi” << endl;  //inserts a newline,then flushes the buffer
```

**2.unitbuf操作符**

如果需要刷新所有输出，最好使用unitbuf操作符。

 

```
cout << unitbuf << “first” << “second” << nonunitbuf; 
```

**3.将输入和输出绑定一起**

任何读输入流的尝试都将首先刷新其输出流关联的缓冲区

 

**四、文件的输入和输出** 

fstream头文件定义了三种支持文件IO的类型：

ifstream,由istream派生而来，提供读文件的功能

ostream,由ostream派生而来，提供写文件的功能

fstream,由iostream派生而来，提供读写同一个文件的功能

 

fstream还定义了两个自己的新操作—open和close

 

**1.文件流对象的使用**

 

```
//假设ifile和ofile是存储希望读写的string对象
ifstream infile(ifile.c_str());
ofstream outfile(ofile.c_str());
//捆绑要读写的文件对象
ifile.open(“in”);
otfile.open(“out”); 
```

IO标准库是使用C风格字符串的 

1)检查文件打开是否成功

if (infile)这种方式来检查

2)将文件流与新文件重新捆绑

 

```
ifstream infile(“in”);
infile.close();
infile.open(“next”);
```

3)清除文件流的状态

如果需要重用文件流读写多个文件，必须在读另外一个文件之前调用clear清楚流的状态

 

**2.文件模式**

![](http://dl.iteye.com/upload/attachment/0072/3951/ad011778-3823-3c1e-898e-027673017fe0.png)

所有文件都可以使用ate或binary模式打开。

为ofstream对象指向out模式等效于同时指定了out和trunc模式。

1)对同一个文件作输入和输出运算

同时打开输入和输出模式：

 

```
fstream inOut (“copyOut”, fstream::in | fstream::out);
```

 

2)模式是文件的属性而不是流的属性

只要调用open函数，就要设置文件模式，其模式的设置可以是显示的也可以是隐式的

3)打开模式的有效组合

![](http://dl.iteye.com/upload/attachment/0072/3955/3c4b918b-e088-384b-affc-3ca0df2e7223.png)

所有组合可以添加ate模式。 

**3.一个打开并检查输入文件的程序**

 

```
Ifstream open_file(ifstream &in, const string &file)
{
              in.close();
              in.clear();
              in.open(file.c_str());
              return in;
} 
```

 

**五、字符串流** 

![](http://dl.iteye.com/upload/attachment/0072/3993/00ecaeb0-7124-3638-93f5-d7edcc25d64f.png)

**1.stringstream对象的使用**

 

```
string line,word;
while (getline(cin,line)){
              istringstream stream(line);
              while(stream >> word){
      //TO DO
}
}
```

**2.stringstream提供的转换和/或格式化**

常见用法是，需要在多种数据类型之间实现自动格式化使用该类类型