您的位置:首页 > 编程语言 > C语言/C++

c++ primer(第五版)笔记 第八章 io

2014-08-28 19:20 429 查看
// iostream 头文件
// istream, wistream 从流读取数据
// ostream, wostream 向流写入数据
// iostream, wiostream 读写流

// fstream 头文件
// ifstream, wifstream 从文件读取数据
// ofstream, wofstream 向文件写入数据
// fstream, wfstream 读写文件

// sstream 头文件
// istringstream, wistringstream 从string 读取数据
// ostringstream, wostringstream 向string 写入数据
// stringstream, wstringstream 读写string

//IO 对象无法拷贝或赋值,所以不能用于形参或返回类型,可以使用引用传递和返回,会改变其状态,也不能是 const

//IO 条件状态(condition state)
// strm 表示上述所有类型
// strm::iostate	iostate 机器相关类型,提供表达条件状态的完整功能
// strm::badbit		表示流已崩溃
// strm::failbit	表示 IO 操作失败
// strm::eofbit		表示流到达了文件结束
// strm::goodbit	表示流未处于错误状态,此值保证为 0
// s.eof()		若流 s 的 eofbit 置位,则返回 true
// s.fail()	若流 s 的 failbit 或 badbit 置位,则返回 true
// s.bad()		若流 s 的 badbit 置位,则返回 true
// s.good()	若流 s 处于有效状态,则返回 true
// s.clear()	将流 s 的所有条件状态位复位,将流的状态设置为有效,返回 void
// s.clear(flags)		根据给定的 flags 位,将流 s 的对应条件状态位复位,返回 void,flags 的类型为 strm::iostate
// s.setstate(flags)	根据给定的 flags 位,将流 s 的对应条件状态位复位,返回 void,flags 的类型为 strm::iostate
// s.rdstate()			返回流 s 的当前条件状态,类型为 strm::iostate

// 最简单的流状态判断,如果操作成功,流保持有效状态,则条件为真
// while (cin >> val)

// 上述条件仅能判断流是否有效,并不知道流的状态,eofbit failbit badbit 都会使上述条件失败
// 如果 badbit 被置位,表示系统级错误,流不可再用
// 如果 failbit 被置位,表示发生的错误可以恢复,流可以继续使用
// 如果到达文件尾,eofbit 和 failbit 都会置位,goodbit 为0,表示流没发生错误
// 方法 good 在所有错误位均未置位时,返回 true,确定流是否正确的有效方法之一,另一种方法是 !fail()
// 方法 fail bad eof 在对应错误位置位时,返回 true,如果 badbit 被置位,fail 也会返回 true

// 缓冲刷新的原因:
// 1>程序正常结束,作为 main 函数中 return 操作的一部分
// 2>缓冲区满时,需要刷新缓冲区,以便后续数据的写入
// 3>使用 endl 显式刷新
// 4>在每个输出操作后,用操作符 unitbuf 设置流的内部状态,默认对 cerr 设置 unitbuf,所以内容是立即刷新的
// 5>一个输出流被关联到另一个流,读写被关联流时,关联的流的缓冲会刷新,默认 cin 和 cerr 关联到 cout,所以读写 cin cerr,会刷新 cout 的缓冲

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using std::istream;
using std::ostream;
using std::cin;
using std::cout;
using std::endl;
using std::ends;
using std::flush;
using std::unitbuf;
using std::nounitbuf;
using std::string;
using std::vector;
using std::ifstream;
using std::istringstream;

istream &io_test(istream &is);
void io_test_state(istream &is);
void read();
void off();
void read_from_file();

int main()
{
auto state = io_test(cin).rdstate();
cout << state << endl;	//输出流的状态, clear 后为0
cout << "==========================================" << endl;
io_test_state(cin);
//刷新输出缓冲
cout << "hello world" << endl;	//输出 hello world 和一个换行符,然后刷新缓冲
cout << "hello world" << ends;	//输出 hello world 和一个空字符,然后刷新缓冲
cout << "hello world" << flush;	//输出 hello world ,然后刷新缓冲,不添加任何字符
cout << "==========================================" << endl;
// unitbuf 操作符,刷新缓冲
cout << unitbuf;
cout << "hello world";
cout << "HELLO WORLD";
cout << nounitbuf << endl;
cout << "==========================================" << endl;
read_from_file();
}

istream &io_test(istream &is)
{
string s;
while(!is.eof())	//遇到eof
{
is >> s;
cout << s << endl;
}
is.clear();
return is;
}

void io_test_state(istream &cin)
{
cout << "before read" << endl;
if (cin.good()) cout << "cin's good" << endl;
if (cin.bad()) cout << "cin's bad" << endl;
if (cin.fail()) cout << "cin's fail" << endl;
if (cin.eof()) cout << "cin's eof" << endl;

read();
cout << "after read" << endl;
if (cin.good()) cout << "cin's good" << endl;
if (cin.bad()) cout << "cin's bad" << endl;
if (cin.fail()) cout << "cin's fail" << endl;
if (cin.eof()) cout << "cin's eof" << endl;

off();
cout << "after off" << endl;
if (cin.good()) cout << "cin's good" << endl;
if (cin.bad()) cout << "cin's bad" << endl;
if (cin.fail()) cout << "cin's fail" << endl;
if (cin.eof()) cout << "cin's eof" << endl;
}

inline void read()
{
cin.setstate(cin.badbit | cin.eofbit | cin.failbit);
}

inline void off()
{
cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit);
}

// 文件输入输出
// fstream 是 fstream 头文件中一个类型
// fstream fstrm;			//创建一个未绑定的文件流
// fstream fstrm(s);		//创建一个未绑定的文件流,并打开名为 s 的文件,s 可以是 string 也可以是 char *[]
// fstream fstrm(s,mode);	//以 mode 的模式执行上面的操作
// fstrm.open(s);			//返回 void,打开名为 s 的文件,并与 fstrm 关联
// fstrm.close();			//返回 void,关闭 fstrm 关联的文件
// fstrm.is_open();		//返回 bool,指明与 fstrm 关联的文件是否成功打开并尚未关闭

//文件模式(file mode)
// in		以读方式打开
// out		以读方式打开
// app		每次写操作前,定位到文件尾
// ate		打开文件后,立即定位到文件尾
// trunc	截断文件
// binary	以二进制方式 io

// 组合规则
// 只有 ifstream 或 fstream 对象才可以设定 in 模式
// 只有 ofstream 或 fstream 对象才可以设定 out 模式
// 只有当 out 也被设定时,才可以设定 trunc
// 只要没设定 trunc,就可以设定 app, 在 app 下,总是以 out 的方式打开
// 默认情况下,没有指定 trunc,以 out 打开的文件也会截断,如果要保留 out 打开的文件内容,须同时指定 app 追加到文件尾,或者 in 读写
// ate 和 binary 可以用在任何类型的文件流对象,可以和其他任何模式组合
void read_from_file()
{
ifstream ifstrm("in");	//创建文件流,并关联文件
string s;
vector<string> vs;
// while(ifstrm >> s)		//读入单词
while(getline(ifstrm, s))	//读入一行
{
vs.push_back(s);
//使用 istringstream 对象输出
istringstream record(s);
string tmp;
while(record >> tmp)
cout << tmp << endl;
}
cout << "....end...." << endl;
// for(auto &s:vs)		//输出显式
// cout << s << endl;

}

//string 流
// sstream 是 sstream 头文件中的类型
// sstream strm;		未绑定的 stringstream 对象
// sstream strm(s);	保存 string s 的一个拷贝
// strm.str();			返回 strm 保存的拷贝
// strm.str(s);		拷贝 s 到 strm 中,返回 void
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: