介绍

// cout: 全局ostream对象,(typedef basic_ostream<char> ostream)
// <<: ostream& ostream::operator<< (string v);
// endl: '\n' + flush {
ofstream of("MyLog.txt"); //如果文件不存在,新建
of << "Experience is the mother of wisdom" << endl;
of << 234 << endl;
of << 2.3 << endl; of << bitset<8>(14) << endl; //00001110
of << complex<int>(2, 3) << endl; //(2, 3)
} //RAII

文件流和错误句柄

  • 输出文件流
ofstream of("MyLog.txt");	//清除文件内容
ofstream of("MyLog.txt", ofstream::app); //将输出指针移到文件末尾
of << "Honesty is the best policy." <<endl; ofstream of("MyLog.txt", ofstream::in | ofstream::out);
of.seekp(10, ios::beg); //将输出指针至文件开头之后的10个字符处
of << "12345"; //覆盖写5个字符
of.seekp(-5, ios::end); //将输出指针移到文件末尾前5个字符处
of.seekp(-5, ios::cur); //将输出指针移到当前位置前5个字符处
  • 输入文件流
ifstream inf("MyLog.txt");
int i;
inf >> i; //读入一个单词, 失败
//错误状态: goodbit, badbit, failbit, eofbit
inf.good(); //一切OK(goodbit == 1)
inf.bad(); //不可恢复的错误(badbit == 1)
inf.fail(); //失败的刘操作,往往可恢复 (failbit == 1, badbit == 1)
inf.eof(); //文件尾(eofbit == 1) inf.clear(); //清除所有错误状态
inf.clear(ios::badbit); //将错误标志设置一个新值 inf.rdstate(); //读当前状态flag
inf.clear(inf.rdstate() & ~ios::failbit); //只清除failbit位 if (inf) //等效于: if (!inf.fail())
cout << "Read successfully";
inf.exceptions(ios::badbit | ios::failbit); //设置异常mask
//badbit或failbit为1时,抛出ios::failure异常
//eofbit为1时,不抛
inf.exception(ios::goodbit); //不抛异常

格式化数据

cout << 34 << endl; //34
cout.setf(ios::oct, ios::basefield);
cout << 34; //42
cout.setf(ios::showbase);
cout << 34; //042
cout.setf(ios::hex, ios::basefield);
cout << 34; //0x22 cout.unsetf(ios::showbase);
cout << 34; //22 cout.setf(ios::dec, ios::basefiled); cout.width(10);
cout << 26 << endl; // 26
cout.setf(ios::left, ios::adjustfield); //26 //浮点数
cout.setf(ios::scientific, ios::floatfield);
cout << 340.1 << endl; //3.401000e+002
cout.setf(ios::fixed, ios::floatfield);
cout << 340.1 << endl; //340.100000
cout.precision(3);
cout << 340.1 << endl; /340.100 int i;
cin.setf(ios::hex, ios::basefield);
cin >> i; //Enter: 12
// i==18
ios::fmtflags f = cout.flags();
cout.flags(ios::oct|ios::showbase); //用来非格式化IO的成员函数
ifstream inf("MyLog.txt");
char buf[80];
inf.get(buf, 80); //读最多80个字符
Inf.getline(buf, 80); //读最多80个字符,或直到'\n'
inf.read(buf, 20); //读20字符
inf.ignore(3);
inf.peek(); //返回string最顶端的字符
inf.unget(); //将最后一个读的字符放回流中 inf.putback('z');
inf.get(); //读一个字符
inf.gcount(); //返回上次非格式化读的字符数 ofstream of("MyLog.txt");
of.put('c');
of.write(buf, 6); //写buf的前6个字符
of.flush(); //刷新输出

操纵符Manipulators

ostream& endl(ostream& sm) {
sm.put('\n');
sm.flush();
return sm;
} ostream& ostream::operator<<(ostream& (*func)(ostream&)) {
return (*func)(*this);
} cout << "Hello" << endl; //endl是函数 cout << ends; // '\0'
cout << flush; //
cin >> ws; //读取并丢弃空白
cout >> setw(8) << left << setfill('_') << 99 << endl; //99______ cout << hex << showbase << 14; //0xe

流缓冲

//	格式化数据  -- 流
// 将数据与外部设备交流 -- 流缓冲
cout << 34;
streambuf* pbuf = cout.rdbuf(); ostream myCout(pbuf);
myCout << 34; // 34输出到标准输出stdout muCout.setf(ios::showpos); //显示符号
myCout.width(20);
myCout << 12 <<endl; // +12
cout << 12 << endl; //12
适合临时修改数据格式,不想修改cout,可能其他人在用 ofstream of("MyLog.txt");
steambuf* origBuf = cout.rdbuf();
cout.rdbuf(of.rdbuf());
cout << "Hello" << endl; //MyLog.txt has "Hello" 重定向 cout.rdbuf(origBuf);
cout << "Goodbye" << endl; //stdout: Googbye // 流缓冲迭代器
istreambuf_iterator<char> i(cin);
osteambuf_iterator<char> o(cout);
while (*i != 'x') {
*o = *i;
++o;
++i;
} copy(istreambuf_iterator<char>(cin), istreambuf_iterator<char>(), ostreambuf_iterator<char>(cout));

字符串流

stringstream ss;	// 没有IO操作的流

ss << 89 << "Hex: " << hex << 89 << " Oct: " << oct << 89;
cout << ss.str() << endl; // 89 Hex: 59 Oct: 131 int a, b, c;
string s1; ss >> hex >> a; //一个token一个token读取,以空格tab和换行分隔,a==137
ss >> s1; // s1: "Hex:"
ss >> dec >> b; // b==59
ss.ignore(6); //忽略接下来6个字符
ss >> oct >> c; // c==89

自定义类中使用流

using namespace std;

struct Dog {
int age_;
string name_;
}; ostream& operator<<(ostream& sm, const Dog& d) {
sm << "My name is " << d.name_ << " and my age is " << d.age_ << endl;
return sm;
} istream& operator>>(istream& sm, Dog& d){
sm >> d.age_;
sm >> d.name_;
return sm;
} int main() {
Dog d{2, "Bob"};
cout << d; cin >> d;
cout << d;
}

STL基础--流的更多相关文章

  1. C++ STL IO流 与 Unicode (UTF-16 UTF-8) 的协同工作

    09年研究技术的大神真的好多,本文测试有很多错误,更正后发布下(可能与编辑器相关). file.imbue(locale(file.getloc(), new codecvt_utf8<wcha ...

  2. STL基础--迭代器和算法

    1 迭代器 Iterators 5种迭代器类型 随机访问迭代器: vector, deque, array // 允许的操作 vector<int> itr; itr = itr + 5; ...

  3. c++中级 STL基础学习(二)

    deque 和vector差不多,可以在前端后端插入,一般用deque取代vector,vector只能在后端插入push_back().deque还可以push_front(),但是deque后端插 ...

  4. Java之IO流基础流对象

    输入流和输出流是相对于内存设备而言 即将外设中的数据读取到内存中就是输入    将内存中的数据写入到外设中就是输出   字符流的由来:     其实就是:字节流读取文字字节数据后,不直接操作而是先查指 ...

  5. STL基础知识

    一,STL的组成 1.什么是STL STL(Standard Template Library)标准模板库的简称,是由惠普开发的一系列软件的总称,STL现在是C++的一部分,已经被构建于编译系统之内, ...

  6. STL基础复习

    stl容器:vector,deque,list,map/multimap,set 特殊容器:stack,queue,priority_queue 通用操作 size()  返回当前容器元素数量 emp ...

  7. STL基础--算法(排序)

    STL排序算法 排序算法要求随机访问迭代器 vector, deque, container array, native array 例子 vector<int> vec = {9,1,1 ...

  8. STL基础--仿函数(函数对象)

    1 首先看个仿函数的例子 class X { public: void operator()(string str) { // 函数调用运算符,返回类型在operator之前 cout << ...

  9. STL基础--容器

    容器种类 序列容器(数组,链表) Vector, deque, list, forward list, array 关联容器(二叉树),总是有序的 set, multiset根据值排序,元素值不能修改 ...

随机推荐

  1. CodeMirror tab转空格

    解决CodeMirror编辑器Tab转空格问题 editor.setOption("extraKeys", { Tab: newTab }); function newTab(cm ...

  2. Js中的判空

    1.JS 中判断 undefined JavaScript 中有两个特殊数据类型:undefined 和 null var test= undefined; if (typeof(test) == u ...

  3. django BBS project login登录功能实现

    1.models from django.db import models # Create your models here. from django.contrib.auth.models imp ...

  4. Python学习之---Python中的内置函数(方法)(更新中。。。)

    add(item)   #将item添加到s中,如果item已经在s中,则无任何效果 break        #退出循环,不会再运行循环中余下的代码 bool()     #将参数转换为布尔型 by ...

  5. C++学习(十二)(C语言部分)之 循环

    上期回顾:分支if else switch 三目运算符 格式:if(条件){语句1:}else{语句2:} switch (变量){default:break;case 常量1:语句1 ;break; ...

  6. Java 枚举(enum) 详解4种常见的用法

    JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 大师兄我又加上自己的理解,来帮助各位理解一下. 用法一:常量 在JDK1.5 之前,我们定义常 ...

  7. MySQL数据库的学习

    ------------------------------------- 管理数据库PS: Mysql的管理 主要是包括 用户的管理. show........各种的信息   SHOW COLUMN ...

  8. Morris

    Morris /*Morris遍历树: *一:如果一个结点有左子树会到达此节点两次(第二次到达结点的时候左子树的所有结点都遍历完成),第一次遍历左子树最后 * 节点为nullptr,第二次遍历指向他自 ...

  9. Task.Delay() 和 Thread.Sleep() 区别

    1.Thread.Sleep 是同步延迟,Task.Delay异步延迟. 2.Thread.Sleep 会阻塞线程,Task.Delay不会. 3.Thread.Sleep不能取消,Task.Dela ...

  10. ML(5)——神经网络3(随机初始化与梯度检验)

    随机初始化 在线性回归和逻辑回归中,使用梯度下降法之前,将θ设置为0向量,有时会习惯性的将神经网络中的权重全部初始化为0,然而这在神经网络中并不适用. 以简单的三层神经网络为例,将全部权重都设置为0, ...