标签:
撒花庆祝下,终于看完了(C++Primer)第一部分,即将进入第二部分!
IO部分,最基本的是iostream(istream、ostream),子类有fstream(ifstream、ofstream)和sstream(istringstream、ostringstream)。
iostream是控制窗口输入输出。
fstream是文件输入输出。
sstream是字符串输入输出(内存中)。
cin.tie(0); //断开默认的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(0); cin.tie(&cerr); //why &? cerr.tie(0); cerr.tie(&cout); string str; cin>>str; cerr<<"hehe";//如何不刷新? cerr<<"hehe";//如何不刷新? cin>>str;//再来一次 cerr<<str; cout<<str; return 0; }
#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 -1; } if(!out){ cerr<<"Error: unable to open file:"<<outfile<<endl; return -1; } return 0; }
//这个代码有问题,忽略吧 #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 0; } 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 0; } 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 0; } 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=100,val2=500; 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 0; }
标签:
原文地址:http://www.cnblogs.com/larryzeal/p/5597163.html