C++ 标准库 stream2

ofstream of("test.txt",ofstream::opp)//将 output pointer 移到文件结尾

of<<"world"<<endl;  //将字符放在文件的结尾

//如果需要访问文件的中间

ofstream of("test.txt",ofstream::in|ofsteam::out);//对文件既需要读也需要写

of.seekp(1,ios::beg);//移到离文件开始1 char 的位置

of<<"222"<<endl; //重写3个char

of.seekp(-5,ios::end);//指针移到离结尾5个char的地方

of<<"sds"<<endl;

of.seekp(-5,ios::cur);//指针移到离当前位置前5个char的地方

ifstream inf("test.txt");

string s;

inf>>s;//

//错误状态: goodbit, badbit, failbit, eofbit

inf.good();

inf.bad();

inf.fail();

inf.eof();

inf.clear()//清除所有的错误

inf.clear(ios::badbit);//设置一个新的值到error flag

inf.rdstate();读取当前的状态位

if(inf)//等价于  if(!inf.fail())

{

          cout<<"blabla..."<<endl;

}

if(inf>>s)//等价于if(inf)  >> 返回istream 的一个引用

{

}

inf .exception(ios::badbit|ios::failbit)//异常处理

{

}

猜你喜欢

转载自blog.csdn.net/xieshangxin/article/details/89415357
今日推荐