C++课件输入输出流课件.ppt
文本预览下载声明
机械工业出版社;第8章 输入输出流;输入输出流;输入输出流;输入输出流;格式化输出
int i=5; double j=12.3456789; float a=34.5;
cout.width(10);
cout.fill(‘*’);
coutiendl;
cout.precision(5);
coutjendl;
cout.setf( ios::scientific | ios::left | ios::showpos);
coutaendl;;输入输出流; fstream类用来处理磁盘文件的输入和输出。ifstream类用来处理磁盘文件的输入操作,ofstream类用来处理磁盘文件的输出操作。
定义文件对象:
ofstream file1;
ifstream file2;
fstream file3;
;磁盘文件的输入输出;例:
file1.seekp(50,ios::beg);
将写指针移到距当前位置50个字节处。
file1.seekp(-50,ios::end);
将写指针移到距文件尾50个字节处。
file1.seekg(-50,ios::cur);
将读指针移到当前位置前50个字节处。
文本文件的读写
流类库的输入输出操作、put()、write()、、get()、getline()等???同样可以用于文件的输入输出。;例1:编程实现将文字”I am a stuent. I am a college student.”分两行写入磁盘文件”text.dat”。;磁盘文件的输入输出;二进制文件的输入输出;#include iostream.h
#include fstream.h
void main()
{ fstream file;
file.open(“text.dat”,ios::in | ios::out | ios::binary);
if(!file)
{ cout”file open error!”endl;
abort();
}
file.write(“I am a student.\n”);
file.write(“I am a college student\n”);
file.seekp(0,ios::beg); //将写指针移到文件头
char ch[50];
while(!file.eof())
{ file.read(ch,sizeof(ch));
coutchendl;
}
file.close();
}
显示全部