8.1 IO类
8.2 文件输入输出
8.3 string流
•1. 输入输出机制
•2. 流类库
•3. 标准输入输出流
•4. 文件输入输出流
•5. 字符串输入输出流
•C++语言的输入输出机制包含3层,前两层是从传统的C语言继承而来,分别是底层I/O和高层I/O,第3层是C++中增添的流类库。
•(1)底层I/O:底层I/O依赖于操作系统来实现,调用操作系统的功能对文件进行输入输出处理,具有较高的速度。底层I/O将外部设备和磁盘文件都等同于逻辑文件,采用相同的方法进行处理,一般过程为“
打开文件
”、“
读写文件”,“
关闭文件”,这些是通过一组底层I/O函数来完成的,这些函数定义在头文件io.h中。
•(2)高层I/O:高层I/O是在底层I/O的基础上扩展起来的,仍旧将外部设备和磁盘文件统一处理,但处理的方式更为灵活,提供的一组处理函数定义在头文件stdio.h中,新的C++标准头文件为<cstdio>,提供的这些函数大体可分为两类:一般文件函数(外部设备和磁盘文件)和标准I/O函数。
除了从C语言中继承了上述两种I/O机制外,C++还特有一种输出机制:流类库(即iostream类库),这是C++所特有的,iostream类库为内置类型对象提供了输入输出支持,也支持文件的输入输出,另外,类的设计者可以通过运算符重载机制对iostream库的扩展,来支持自定义类型的输入输出操作。
•流的概念:
可以把流看做“水管”,这样输入流指的是从程序外部输入数据,类似于水通过水管流进程序,输出流恰好相反,是从程序内部输出到外部。这里要注意的是,输入输出的参照物是程序。
•关于“流”,可以学院派地解释为“流是(表达)读写数据的一种可移植的方法,它为一般的I/O操作提供了灵活有效的手段。一个流是一个由指针操作的文件或者是一个物理设备,而这个指针正是指向了这个流。”
就C++程序而言, I/O操作可以简单地看作是从程序移进或移出字节,这种搬运的过程便称为流(stream)。程序只需要关心是否正确地输出了字节数据,以及是否正确地输入了要读取字节数据,特定I/O设备的细节对程序员是隐藏的
•iostream库主要包括如下图所示的几个头文件,在前面所有示例代码中使用的头文件<iostream>便是其中之一
iostream 的继承层次:
8.1 IO类
<iostream>
•流的状态:
–
bad
bit 系统级故障,不可恢复
–
fail
bit 可以恢复的错误
–
eof
bit 碰到了文件结尾
–
good
bit有效状态
•查询流的状态
–
cin.bad
()
–
cin.fail
()
–
cin.eof
()
–
cin.good
()
重置状态 cin.clear()
#include <iostream>
#include <string>
void printCin()
{
std::cout << "bad = " << std::cin.bad() << std::endl;
std::cout << "fail = " << std::cin.fail() << std::endl;
std::cout << "eof = " << std::cin.eof() << std::endl;
std::cout << "good = " << std::cin.good() << std::endl
<< std::endl;
}
int main(void)
{
int inum;
printCin();
while(std::cin >> inum)
{
std::cout << inum << std::endl;
}
printCin();
std::cin.clear();
printCin();
//std::cin.ignore(1024, '\n');
std::string s;
std::cin >> s;
std::cout << s << std::endl;
return 0;
}
8.2文件输入输出
<fstream> <ifstream> <ofstream>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int test0(void)
{
std::ifstream ifs(“io1.cc”);//定义并打开了一个ifstream对象
if(!ifs.good())
{
std::cout << "open file error!" << std::endl;
return -1;
}
//std::string s;
//while(ifs >> s)
//{
// std::cout << s <<std::endl;
//}
std::vector<std::string> vec_str;
std::string line;
while(getline(ifs, line))
{
//std::cout << line << std::endl;
vec_str.push_back(line);
}
std::vector<std::string>::iterator sit = vec_str.begin();
for(; sit != vec_str.end(); ++sit)
{
std::cout << *sit << std::endl;
}
ifs.close();
return 0;
}
int main(void)
{
std::ifstream ifs("io1.cc");
if(!ifs.good())
{
std::cout << "open file error!" << std::endl;
return -1;
}
std::ofstream ofs("test.txt");
std::string line;
while(getline(ifs, line))
{
ofs << line << std::endl;
}
return 0;
}
•文件模式
ios::in 打开文件做读操作
ios::out 打开文件做写操作,会删除原有数据
ios::app 在每次写之前找到文件尾
ios::trunc 打开文件时清空已存在的文件流
ios::ate 打开文件后立即定位到文件末尾
ios::binary以二进制模式进行IO操作
#include <iostream>
#include <fstream>
int test0(void)
{
//std::ofstream ofs("text1.txt", std::ios::ate | std::ios::app);
//std::ofstream ofs("text1.txt", std::ios::ate);
std::ofstream ofs("text1.txt", std::ios::app);
if(!ofs)
{
std::cout << "ofstream error!" << std::endl;
return -1;
}
std::cout << ofs.tellp() << std::endl;
ofs << "that's new" << std::endl;
ofs.close();
return 0;
}
int test1(void)
{
test0();
std: :ifstream ifs("text1.txt", std::ios::ate);
if(!ifs)
{
std::cout << "ifstream error" << std::endl;
return -1;
}
std::cout << ifs.tellg() << std::endl;
ifs.close();
return 0;
}
int main(void)
{
std::fstream outfile("f1.dat", std::ios::out | std::ios::in);
if(!outfile)
{
std::cout << "fstream error" << std::endl;
return -1;
}
int ival;
for(int idx = 0; idx != 10; ++idx)
{
std::cin >> ival;
outfile << ival << ' ';
}
std::cout << outfile.tellp() << std::endl;
outfile.seekg(0, std::ios::beg);
for(int idx = 0; idx != 10; ++idx)
{
outfile >> ival;
std::cout << ival << ' ';
}
outfile.close();
return 0;
}
8.3 string流
sstream头文件定义了三个类型来支持内存IO,这些类型可以向string写入数据,从string读取数据,就像string是一个IO流一样、
#include <iostream>
#include <sstream>
#include <string>
int test0(void)
{
int ival = 512;
int ival2 = 1024;
std::stringstream ss;
ss << "ival= " << ival << " " << "ival2= " << ival2 << std::endl;
std::string str = ss.str();
std::cout << str << std::endl;
while(ss >> str)
{
std::cout << str << std::endl;
}
return 0;
}