C++ IO流小结
撒花庆祝下,终于看完了(C++Primer)第一部分,即将进入第二部分!
IO部分,最基本的是iostream(istream、ostream),子类有fstream(ifstream、ofstream)和sstream(istringstream、ostringstream)。
iostream是控制窗口输入输出。
fstream是文件输入输出。
sstream是字符串输入输出(内存中)。
- cin.tie(); //断开默认的tie(默认cin.tie(&cout))
- cin.tie(&cerr); //重新绑定
- // construct an ifstream and bind it to the file named ifile
- ifstream infile(ifile.c_str());
- // ofstream output file object to write file named ofile
- ofstream outfile(ofile.c_str());
- ifstream infile;
- ofstream outfile;
- infile.open(ifile.c_str());
- outfile.open(ofile.c_str());
- #include <iostream>
- #include <string>
- using namespace std;
- int main(){
- cout<<"endl!"<<endl;
- cout<<"flush!"<<flush;//
- cout<<"null-terminated ends!"<<ends;
- cout<<"\r\n";
- //一次性刷新所有输出
- cout<<unitbuf<<"first"<<" second"<<nounitbuf; //cout<<"first"<<flush<<"second"<<flush;
- cout<<"\r\n";
- ///=----------------------
- ostream *old_tie = cin.tie();
- cout<<"old_tie:"<<old_tie<<endl;
- cin.tie();
- cin.tie(&cerr); //why &?
- cerr.tie();
- cerr.tie(&cout);
- string str;
- cin>>str;
- cerr<<"hehe";//如何不刷新?
- cerr<<"hehe";//如何不刷新?
- cin>>str;//再来一次
- cerr<<str;
- cout<<str;
- return ;
- }
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int main(){
- string infile="abc";
- string outfile="abc";
- ifstream in(infile.c_str());
- ofstream out(outfile.c_str());
- if(!in){
- cerr<<"Error: unable to open file:"<<infile<<endl;
- return -;
- }
- if(!out){
- cerr<<"Error: unable to open file:"<<outfile<<endl;
- return -;
- }
- return ;
- }
- //这个代码有问题,忽略吧
- #include <iostream>
- #include <string>
- #include <stdexcept>
- using namespace std;
- void run1();
- void run2();
- istream &getistream(istream &in);
- int main(){
- run1();
- // run2();
- //
- // getistream(cin);//注意,io流不可复制或赋值。
- // if(cin){
- // cout<<"OK!"<<endl;
- // }else{
- // cout<<"sth is wrong"<<endl;
- // if(cin.eof()){
- // cout<<"eof was not cleared!"<<endl;
- // }
- // if(cin.bad()){
- // cout<<"bad was not cleared!"<<endl;
- // }
- // if(cin.fail()){
- // cout<<"fail was not cleared!"<<endl;
- // }
- // return -1;
- // }
- return ;
- }
- void run1(){
- int val;
- while(cin>>val, !cin.eof()){//有输入,不是结尾,就继续 。逗号运算符,返回右边的结果
- if(cin.bad()){
- cout<<"input stream damaged!"<<endl;
- throw runtime_error("IO stream corrupted");
- }
- if(cin.fail()){
- cerr<<"bad input!"<<endl;
- // cin.clear(istream::failbit);//
- // cin.clear();
- // cin.clear(istream::goodbit);
- cin.clear(istream::failbit);
- cin.clear(istream::badbit);
- cin.clear(istream::eofbit);
- // cin.clear();//why this does not work?
- // cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- cout<<"try again.."<<endl;
- continue;
- }
- cout<<val<<endl;
- }
- }
- void run2(){
- string str;
- cout<<"type sth .."<<endl;
- cin>>str;
- iostream::iostate old_state= cin.rdstate();//
- cout<<old_state<<endl;
- cin.clear();//what?
- //...
- cin.clear(old_state);//清除旧状态,而非设置旧状态!设置应该是setstate。这里书上弄错了吧。
- }
- istream &getistream(istream &in){
- // iostream::iostate old_state = in.rdstate();
- cout<<"type sth.."<<endl;
- string str;
- while(in>>str, !in.eof()){
- //
- if( in.fail() || in.bad()){
- cerr<<"fail or bad";
- in.clear();
- continue;
- }
- cout<<"you typed: "<<str<<endl;
- }
- // in.setstate(old_state);
- // in.clear(old_state);//for what?
- in.clear();//恢复正常
- return in;
- }
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- ifstream &openfile(ifstream &in, string &filename);
- ofstream &savefile(ofstream &out, string filename);
- //复制文件练习
- int main(){
- string filename="e:/DbUtil.java";
- string filename2="e:/DbUtil1.java";
- ifstream in;
- ofstream out;
- openfile(in, filename);
- savefile(out,filename2);
- string line;
- while(getline(in, line)){
- out<<line<<endl;
- }
- in.close();
- out.close();
- return ;
- }
- ifstream &openfile(ifstream &in, string &filename){
- in.close();
- in.clear();
- in.open(filename.c_str());
- return in;
- }
- ofstream &savefile(ofstream &out, string filename){
- out.close();
- out.clear();
- out.open(filename.c_str(),ofstream::app);
- return out;
- }
- fstream inOut("xxx", fstream::in | fstream::out);
ate是个什么鬼?app会在每次写操作之前都把写指针置于文件末尾,而ate模式则只在打开时才将写指针置于文件末尾。ate模式在文件操作过程中,可以通过seekp等操作移动指针位置。
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- using namespace std;
- ifstream &openfile(ifstream &in, string &filename);
- int main(){
- string filename="e:/DbUtil.java";
- ifstream in;
- openfile(in, filename);
- string line, word;
- while(getline(in, line)){
- istringstream strin(line);
- ostringstream strout(line);
- while(strin>>word){
- strout<<word<<endl;//输出到string了,看不到
- cout<<word<<endl;//输出string到控制台
- }
- }
- in.close();
- in.clear();
- return ;
- }
- ifstream &openfile(ifstream &in, string &filename){
- in.close();
- in.clear();
- in.open(filename.c_str());
- return in;
- }
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- using namespace std;
- //字符串格式转换
- int main(){
- int val1=,val2=;
- string line, word;
- ostringstream out;
- out<<"val1: "<<val1<<"\n"
- <<"val2: "<<val2<<"\n";
- cout<<out.str()<<endl;
- string dump;
- istringstream in(out.str());
- in>>dump>>val1>>dump
- >>dump>>val2>>dump;
- cout<<val1<<"--"<<val2<<endl;
- return ;
- }
C++ IO流小结的更多相关文章
- java IO 流小结
java IO 流小结 java流类图结构 流的分类 按方向 输入流 输出流 按类型 字节流 字符流 结论:只要是处理纯文本数据,就优先考虑使用字符流. 除此之外都使用字节流.
- java学习笔记 --- IO流小结
IO流 |--字节流 |--字节输入流 InputStream int read():一次读取一个字节 int read(byte[] bys):一次读取一个字节数 ...
- Java API —— IO流小结
练习题: 1.复制文本文件 package cn.itcast_01; import java.io.BufferedReader; import java.io.BufferedWriter; im ...
- (27)IO流小结
字节流 输入字节流: ---------| InputStream 所有输入字节流的基类. 抽象类 ------------| FileInputStream 读取文件的输入字节流 --------- ...
- java 21 - 7 IO流小结的图解
- java基础(二十)IO流(三)
这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...
- JavaSE_ IO流 总目录(19~22)
JavaSE学习总结第19天_IO流119.01 集合的特点和数据结构总结19.02 如何选择使用哪种集合19.03 集合常见功能和遍历方式总结19.04 异常的概述和分类19.05 JVM默认处理异 ...
- 【重学Java】IO流
IO流的UML类图 File类 File类概述和构造方法[应用] File类介绍 它是文件和目录路径名的抽象表示 文件和目录是可以通过File封装成对象的 对于File而言,其封装的并不是一个真正存在 ...
- Java(36)IO流案例与总结
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228455.html 博客主页:https://www.cnblogs.com/testero ...
随机推荐
- STL之内存处理
说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL中与内存配置相关的类是allocator类,头文件为:#include<alllocator>这是一个模板类,用于内存的分配.对象 ...
- Cocos2d-x动画工具类
1.此工具类的目的是为了方便运行动画.使用TexturePackerGUI工具能够导出plist文件和png图片,这里我演示样例图片叫bxjg.plist和bxjg.png ///////////// ...
- ubuntu14.4安装gtx970显卡驱动的艰辛历程
1 说明: 本人机器说明,显卡gtx970,内存4G,原来系统是win7 64的.最近要学习机器学习的一些算法,需要安装ubuntu.不过安装环境这一条道路真的很曲折,来来回回弄了好久.以下说一下配置 ...
- 已有iptables表的查看
查看已有iptables表 iptables -L -nv --line-number-L是--list的简写,作用是列出规则-n是ip以数字方式显示,有的会用域名方式显示.-v是显示详细信息 v=v ...
- sqlite时间戳转时间语句(时间转时间戳)实例
sqlite时间戳转时间.时间转时间戳的方法 实现代码: sqlite, 'unixepoch', 'localtime'); +----------------------------------- ...
- 易懂的modelsim学习笔记
1. 建一个总文件夹,如cnt2. 为源代码,测试台文件,仿真各建一文件夹.如src,tb,sim3. 编写源代码,testbench.如cnt.v,tb_cnt.v文件,同时文件名里的模块名与文件名 ...
- 【Android】19.3 ContentProvider及安卓进一步封装后的相关类
分类:C#.Android.VS2015: 创建日期:2016-03-08 一.简介 ContentProvider:内容提供程序. Android的ContentProvider与.NET框架的EF ...
- 在Windows上开发PHP扩展模块
环境: window + php + apache + vc6 + cygwin 下载:php二进制文件: php-5.3.10-Win32-VC9-x86 php源码包:php-5.3 ...
- lua连续随机数
号外:惭愧,工作后几乎没有写博客了,其实是有时间的(每周单休),只是厌烦对着屏幕了,还有懒. 现在老板换人了,时间会多点,估计正常就每周双休了,决定还是每周写两篇(不一定是love2d), 写不出就翻 ...
- ubuntu文件夹默认列表显示
编辑-->首选项-->视图-->列表视图