撒花庆祝下,终于看完了(C++Primer)第一部分,即将进入第二部分!

IO部分,最基本的是iostream(istream、ostream),子类有fstream(ifstream、ofstream)和sstream(istringstream、ostringstream)。

iostream是控制窗口输入输出。

fstream是文件输入输出。

sstream是字符串输入输出(内存中)。

如果两种类型存在继承关系,则可以说一个类“继承”了其父类的行为——接口。
C++ 中所提及的父类称为基类(base class) ,而继承而来的类 则称为派生类(derived class)

 
IO 类型在三个独立的头文件中定义:
    iostream 定义读写控制窗口的类型, fstream 定义读写已命名文件的类型,而 sstream 所定义的类型则用于读写存储在内存中的 string 对象。
    在 fstream 和 sstream 里定义的每种类型都是从 iostream 头文件中定义的相关类型派生而来。
wchar_t、wiostream。。。
 
IO流不允许赋值或者复制
所以,
①函数形参和返回值不能是IO流对象,必须是引用,而且形参不能是const--因为读写都会改变状态。
②只有支持复制的元素类型可以存储在 vector 或其他容器类型里。由于流对象不能复制,因此不能存储在 vector (或其他)容器中 (即不存在存储流对象的 vector 或其他容器)。

所有的流对象,都包含一个条件状态成员,该成员由setstate clear 操作管理。该成员类型为iostate 。另外有三个常量,代表三种状态,可进行位操作。badbitfailbiteofbit
流的状态检查操作则有:bad、fail、eof、good。如果bad、fail、eof中任一个返回true,则流处于错误状态。否则good操作返回true。
clear和setstate操作用于改变条件成员的状态。clear会重设为有效状态。setstate用于打开某个条件,用于表示某个问题的发生。setstate会保留其他状态。
 
 
IO缓冲区每个io流对象都管理一个缓冲区!用于存储程序读写的数据。
除了自动刷新缓冲区之外,还可以手动刷新缓冲区:endl、flush、ends。
另外,如果想让io流刷新输出每次读写的数据,可以使用unitbuf
cout<<unitbuf<<"first"<<" second"<<nounitbuf ; //相当于cout<<"first"<<flush<<"second"<<flush ;
 
警告:如果程序崩溃了,则不会刷新缓冲区!!    这个特性可以用来查找错误,只能是在最后输出语句的后面。
 
----------------------------------
当输入流和输出流绑到一起时,任何读输入流的尝试,都将刷新其输出流关联的缓冲区。
标准库将cout和cin绑定到一起,所以,任何cin语句,都会导致cout缓冲区的刷新。
注意:只是刷新!没有传递数据!!!且一个对象只能绑定一个对象。
 
交互式系统通常应确保它们的输入和输出流是绑在一起的。这样可以保证任何输出都是在试图读之前进行。
  1. cin.tie(); //断开默认的tie(默认cin.tie(&cout))
  2. cin.tie(&cerr); //重新绑定
----------------------------------
 
文件操作:
fstream 类型除了继承下来的行为外,还定义了两个自己的新操作—— open close,以及形参为要打开的文件名的构造函数。

需要读写文件时,则必须定义自己的对象,并将它们绑定在需要的文件上。
假设 ifile 和 ofile 是存储希望读写的文件名的 strings 对象,可如下编写代码: 
  1. // construct an ifstream and bind it to the file named ifile
  2. ifstream infile(ifile.c_str());
  3. // ofstream output file object to write file named ofile
  4. ofstream outfile(ofile.c_str());
也可以如下编写代码:
  1. ifstream infile;
  2. ofstream outfile;
  3.  
  4. infile.open(ifile.c_str());
  5. outfile.open(ofile.c_str());
 
为了实现读写,需要将指定的文件打开并定位,open 函数完成系统指定的所有需要的操作。
 
打开文件后,检查是否正确打开是个好习惯:if(!infile)...//如果没打开。
 
注意:历史原因,只能使用C风格字符串作为文件名。
 
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7.  
  8. cout<<"endl!"<<endl;
  9. cout<<"flush!"<<flush;//
  10. cout<<"null-terminated ends!"<<ends;
  11.  
  12. cout<<"\r\n";
  13. //一次性刷新所有输出
  14. cout<<unitbuf<<"first"<<" second"<<nounitbuf; //cout<<"first"<<flush<<"second"<<flush;
  15.  
  16. cout<<"\r\n";
  17. ///=----------------------
  18. ostream *old_tie = cin.tie();
  19. cout<<"old_tie:"<<old_tie<<endl;
  20. cin.tie();
  21. cin.tie(&cerr); //why &?
  22.  
  23. cerr.tie();
  24. cerr.tie(&cout);
  25.  
  26. string str;
  27. cin>>str;
  28. cerr<<"hehe";//如何不刷新?
  29. cerr<<"hehe";//如何不刷新?
  30.  
  31. cin>>str;//再来一次
  32. cerr<<str;
  33. cout<<str;
  34.  
  35. return ;
  36. }
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8. string infile="abc";
  9. string outfile="abc";
  10. ifstream in(infile.c_str());
  11. ofstream out(outfile.c_str());
  12.  
  13. if(!in){
  14. cerr<<"Error: unable to open file:"<<infile<<endl;
  15. return -;
  16. }
  17. if(!out){
  18. cerr<<"Error: unable to open file:"<<outfile<<endl;
  19. return -;
  20. }
  21.  
  22. return ;
  23. }
  1. //这个代码有问题,忽略吧
  2. #include <iostream>
  3. #include <string>
  4. #include <stdexcept>
  5.  
  6. using namespace std;
  7.  
  8. void run1();
  9. void run2();
  10. istream &getistream(istream &in);
  11.  
  12. int main(){
  13. run1();
  14. // run2();
  15. //
  16. // getistream(cin);//注意,io流不可复制或赋值。
  17. // if(cin){
  18. // cout<<"OK!"<<endl;
  19. // }else{
  20. // cout<<"sth is wrong"<<endl;
  21. // if(cin.eof()){
  22. // cout<<"eof was not cleared!"<<endl;
  23. // }
  24. // if(cin.bad()){
  25. // cout<<"bad was not cleared!"<<endl;
  26. // }
  27. // if(cin.fail()){
  28. // cout<<"fail was not cleared!"<<endl;
  29. // }
  30. // return -1;
  31. // }
  32.  
  33. return ;
  34. }
  35.  
  36. void run1(){
  37. int val;
  38. while(cin>>val, !cin.eof()){//有输入,不是结尾,就继续 。逗号运算符,返回右边的结果
  39. if(cin.bad()){
  40. cout<<"input stream damaged!"<<endl;
  41. throw runtime_error("IO stream corrupted");
  42. }
  43. if(cin.fail()){
  44. cerr<<"bad input!"<<endl;
  45. // cin.clear(istream::failbit);//
  46. // cin.clear();
  47. // cin.clear(istream::goodbit);
  48. cin.clear(istream::failbit);
  49. cin.clear(istream::badbit);
  50. cin.clear(istream::eofbit);
  51. // cin.clear();//why this does not work?
  52.  
  53. // cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  54. cout<<"try again.."<<endl;
  55. continue;
  56. }
  57. cout<<val<<endl;
  58. }
  59. }
  60.  
  61. void run2(){
  62. string str;
  63. cout<<"type sth .."<<endl;
  64. cin>>str;
  65. iostream::iostate old_state= cin.rdstate();//
  66. cout<<old_state<<endl;
  67. cin.clear();//what?
  68. //...
  69. cin.clear(old_state);//清除旧状态,而非设置旧状态!设置应该是setstate。这里书上弄错了吧。
  70.  
  71. }
  72.  
  73. istream &getistream(istream &in){
  74. // iostream::iostate old_state = in.rdstate();
  75. cout<<"type sth.."<<endl;
  76.  
  77. string str;
  78. while(in>>str, !in.eof()){
  79. //
  80. if( in.fail() || in.bad()){
  81. cerr<<"fail or bad";
  82. in.clear();
  83. continue;
  84. }
  85. cout<<"you typed: "<<str<<endl;
  86. }
  87. // in.setstate(old_state);
  88. // in.clear(old_state);//for what?
  89. in.clear();//恢复正常
  90. return in;
  91. }
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. ifstream &openfile(ifstream &in, string &filename);
  8. ofstream &savefile(ofstream &out, string filename);
  9.  
  10. //复制文件练习
  11. int main(){
  12. string filename="e:/DbUtil.java";
  13. string filename2="e:/DbUtil1.java";
  14. ifstream in;
  15. ofstream out;
  16. openfile(in, filename);
  17. savefile(out,filename2);
  18.  
  19. string line;
  20. while(getline(in, line)){
  21. out<<line<<endl;
  22. }
  23. in.close();
  24. out.close();
  25.  
  26. return ;
  27. }
  28.  
  29. ifstream &openfile(ifstream &in, string &filename){
  30. in.close();
  31. in.clear();
  32.  
  33. in.open(filename.c_str());
  34.  
  35. return in;
  36. }
  37.  
  38. ofstream &savefile(ofstream &out, string filename){
  39. out.close();
  40. out.clear();
  41.  
  42. out.open(filename.c_str(),ofstream::app);
  43. return out;
  44. }
----------------------------------
 
 
打开文件需要指定打开的mode,这是文件自身的属性!
mode:in、out、app、ate、trunc、binary。
单纯以out打开,会丢失所有内容。类似out|trunc。
fstream对象默认以in|out模式打开,不清空!!!
  1. fstream inOut("xxx", fstream::in | fstream::out);

ate是个什么鬼?app会在每次写操作之前都把写指针置于文件末尾,而ate模式则只在打开时才将写指针置于文件末尾。ate模式在文件操作过程中,可以通过seekp等操作移动指针位置。

 
------------------------------------------------
sstream,用于从内存中的字符串读取数据,或,向内存中的字符串写入数据!
也可以用于格式转换!!!其实就是通过特定类型的变量来接收!
 
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8. ifstream &openfile(ifstream &in, string &filename);
  9.  
  10. int main(){
  11. string filename="e:/DbUtil.java";
  12. ifstream in;
  13. openfile(in, filename);
  14.  
  15. string line, word;
  16.  
  17. while(getline(in, line)){
  18. istringstream strin(line);
  19. ostringstream strout(line);
  20. while(strin>>word){
  21. strout<<word<<endl;//输出到string了,看不到
  22. cout<<word<<endl;//输出string到控制台
  23. }
  24. }
  25. in.close();
  26. in.clear();
  27.  
  28. return ;
  29. }
  30.  
  31. ifstream &openfile(ifstream &in, string &filename){
  32. in.close();
  33. in.clear();
  34.  
  35. in.open(filename.c_str());
  36.  
  37. return in;
  38. }
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. //字符串格式转换
  10. int main(){
  11.  
  12. int val1=,val2=;
  13. string line, word;
  14. ostringstream out;
  15. out<<"val1: "<<val1<<"\n"
  16. <<"val2: "<<val2<<"\n";
  17.  
  18. cout<<out.str()<<endl;
  19.  
  20. string dump;
  21.  
  22. istringstream in(out.str());
  23. in>>dump>>val1>>dump
  24. >>dump>>val2>>dump;
  25.  
  26. cout<<val1<<"--"<<val2<<endl;
  27.  
  28. return ;
  29. }
 
 注意:i或o是相对内存来说的。将内容读入内存,是in;将内存中的内容输出,是out。
 
 
 
参考:
 

C++ IO流小结的更多相关文章

  1. java IO 流小结

    java IO 流小结 java流类图结构 流的分类 按方向 输入流 输出流 按类型 字节流 字符流 结论:只要是处理纯文本数据,就优先考虑使用字符流. 除此之外都使用字节流.

  2. java学习笔记 --- IO流小结

    IO流  |--字节流    |--字节输入流     InputStream      int read():一次读取一个字节      int read(byte[] bys):一次读取一个字节数 ...

  3. Java API —— IO流小结

    练习题: 1.复制文本文件 package cn.itcast_01; import java.io.BufferedReader; import java.io.BufferedWriter; im ...

  4. (27)IO流小结

    字节流 输入字节流: ---------| InputStream 所有输入字节流的基类. 抽象类 ------------| FileInputStream 读取文件的输入字节流 --------- ...

  5. java 21 - 7 IO流小结的图解

  6. java基础(二十)IO流(三)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  7. JavaSE_ IO流 总目录(19~22)

    JavaSE学习总结第19天_IO流119.01 集合的特点和数据结构总结19.02 如何选择使用哪种集合19.03 集合常见功能和遍历方式总结19.04 异常的概述和分类19.05 JVM默认处理异 ...

  8. 【重学Java】IO流

    IO流的UML类图 File类 File类概述和构造方法[应用] File类介绍 它是文件和目录路径名的抽象表示 文件和目录是可以通过File封装成对象的 对于File而言,其封装的并不是一个真正存在 ...

  9. Java(36)IO流案例与总结

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228455.html 博客主页:https://www.cnblogs.com/testero ...

随机推荐

  1. STL之内存处理

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL中与内存配置相关的类是allocator类,头文件为:#include<alllocator>这是一个模板类,用于内存的分配.对象 ...

  2. Cocos2d-x动画工具类

    1.此工具类的目的是为了方便运行动画.使用TexturePackerGUI工具能够导出plist文件和png图片,这里我演示样例图片叫bxjg.plist和bxjg.png ///////////// ...

  3. ubuntu14.4安装gtx970显卡驱动的艰辛历程

    1 说明: 本人机器说明,显卡gtx970,内存4G,原来系统是win7 64的.最近要学习机器学习的一些算法,需要安装ubuntu.不过安装环境这一条道路真的很曲折,来来回回弄了好久.以下说一下配置 ...

  4. 已有iptables表的查看

    查看已有iptables表 iptables -L -nv --line-number-L是--list的简写,作用是列出规则-n是ip以数字方式显示,有的会用域名方式显示.-v是显示详细信息 v=v ...

  5. sqlite时间戳转时间语句(时间转时间戳)实例

    sqlite时间戳转时间.时间转时间戳的方法 实现代码: sqlite, 'unixepoch', 'localtime'); +----------------------------------- ...

  6. 易懂的modelsim学习笔记

    1. 建一个总文件夹,如cnt2. 为源代码,测试台文件,仿真各建一文件夹.如src,tb,sim3. 编写源代码,testbench.如cnt.v,tb_cnt.v文件,同时文件名里的模块名与文件名 ...

  7. 【Android】19.3 ContentProvider及安卓进一步封装后的相关类

    分类:C#.Android.VS2015: 创建日期:2016-03-08 一.简介 ContentProvider:内容提供程序. Android的ContentProvider与.NET框架的EF ...

  8. 在Windows上开发PHP扩展模块

    环境: window + php + apache + vc6 + cygwin 下载:php二进制文件: php-5.3.10-Win32-VC9-x86        php源码包:php-5.3 ...

  9. lua连续随机数

    号外:惭愧,工作后几乎没有写博客了,其实是有时间的(每周单休),只是厌烦对着屏幕了,还有懒. 现在老板换人了,时间会多点,估计正常就每周双休了,决定还是每周写两篇(不一定是love2d), 写不出就翻 ...

  10. ubuntu文件夹默认列表显示

    编辑-->首选项-->视图-->列表视图