C++第08章输入输出流对象(2013年12月17日)分解.ppt

C++第08章输入输出流对象(2013年12月17日)分解.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* 文件的随机读写:文件打开以后,系统自动生成两个隐含的流指针:读指针和写指针。在输入文件流类中,有关读指针的函数如下: 移动读指针函数 istream istream::seekg(streampos pos); 该函数的功能是将输入文件的指针移动到pos指定的位置中。 文件的读写 * istream istream::seekg(streamoff offset,seek_dir origin); 其中,origin的类型seek_dir是一个枚举类型,有以下三种取值: ios::beg 表示指针的起始位置为文件头 ios::cur 表示指针的起始位置为当前位置 ios::end 表示指针的起始位置为文件尾 该函数的功能是从origin指定的开始位置起,将文件指针移动offset个字节数。 文件的读写 * 返回读指针当前指向的位置值 ? streampos istream::tellg( ); 该函数的功能是确定文件指针的当前位置。 文件的读写 * 相应地,ostream类提供有关写指针的函数如下: 移动写指针函数 ostream ostream::seekp(streampos pos); ostream ostream::seekp(streamoff offset,seek_dir origin); 返回写指针当前指向的位置值 streampos ostream::tellp( ); 函数参数的意义与读指针函数一样。 文件的读写 * 例8-17 文件的随机读写 int main() { fstream file(exam.dat,ios::in|ios::out|ios::binary); if(!file) { coutexam.dat can’t openendl; abort(); } for(int i=0;i15;i++) file.write((char*)i,sizeof(int)); streampos pos=file.tellp(); coutcurrent byte number: posendl; for(i=15;i45;i++) file.write((char*)i,sizeof(int)); file.seekg(pos); file.read((char*)i,sizeof(int)); coutThe data stored is iendl; file.seekp(0,ios::beg); for(i=80;i100;i++) file.write((char*)i,sizeof(int)); file.seekg(pos); file.read((char *)i,sizeof(int)); coutThe data stored is iendl; file.seekp(20,ios::cur); file.read((char *)i,sizeof(int)); coutThe data stored isiendl; coutcurrent byte number: file.tellp()endl; return 0; } current byte number:60 The data stored is 15 The data stored is 95 The data stored is 21 current byte number:88 * int main() { int x=77; cout12345678901234567890\n; cout.fill(#); cout.width(10); coutx=; cout.width(10); cout.setf(ios::left); cout.fill($); coutx\n; int y=0x2a; cout12345678901234567890\n; cout.unsetf(ios::left); cout.fill(%); cout.width(10); couty=; cout.unsetf(ios::right); cout.width(10); cout.setf(ios::left); cout.fill($); couty\n; return 0; } * int main() { int a=5,b=7,c=-1; float x=67.8564,y=-789.124; char ch=A; long n=1234567; unsigned u=65535; coutabendl; coutsetw(3)asetw(3)b\n; coutx,yendl; coutsetw(10)x,setw

文档评论(0)

bbnm58850 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档