8.1 IO类
8.2 文件输入输出
8.3 string流
除了从C语言中继承了上述两种I/O机制外,C++还特有一种输出机制:流类库(即iostream类库),这是C++所特有的,iostream类库为内置类型对象提供了输入输出支持,也支持文件的输入输出,另外,类的设计者可以通过运算符重载机制对iostream库的扩展,来支持自定义类型的输入输出操作。
可以把流看做“水管”,这样输入流指的是从程序外部输入数据,类似于水通过水管流进程序,输出流恰好相反,是从程序内部输出到外部。这里要注意的是,输入输出的参照物是程序。
就C++程序而言, I/O操作可以简单地看作是从程序移进或移出字节,这种搬运的过程便称为流(stream)。程序只需要关心是否正确地输出了字节数据,以及是否正确地输入了要读取字节数据,特定I/O设备的细节对程序员是隐藏的
8.1 IO类
<iostream>
重置状态 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;
}
#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;
}