设为首页 加入收藏

TOP

二级C++输入输出流:磁盘文件的输入和输出
2014-11-19 04:58:04 】 浏览:7187
Tags:二级 输入 输出 磁盘 文件

1、 磁盘文件的打开和关闭操作:


磁盘文件的打开和关闭操作一般使用 fstream 类中所定义的成员函数 open() 和 close() 。


⒈打开文件:


步骤:①先说明一个 fstream 类的对象。


用成员函数 open() 打开指定的文件。


打开指定文件的方式(即文件访问方式)


in: 以输入(读)方式打开文件。


Out: 以输出(写)方式打开文件。


App: 以输出追加方式打开文件。


Ate: 文件打开时,文件指针位于文件尾。


Trunc: 如果文件存在,将其长度截断为 0 ,并清除原有内容;如果文件不存在,则创建新文件。


Binary: 以二进制方式打开文件,缺省时为文本文件。


Nocreate: 打开一个已有文件,如该文件不存在,则打开失败。


Noreplace : 如果文件存在,除非设置 ios::ate 或 ios::app, 否则打开操作失败。


Ios::out|ios::binary: 以二进制写方式打开文件。


Ios::in|ios::binary: 以二进制读方式打开文件。


例如: fstream outfile;


outfile.open(“f1.txt”,ios::out);


或也可以这样打开: fstream outfile(“f1.txt”,ios::out);


还可以用下述方法 表示打开某个写文件:


ofstream ostream(“f1.txt”);


或 ofstream ostrm;


ostrm.open(“f1.txt”);


可以用下述方法表示打开某个读文件:


ifstream istrm(“f2.txt”);


或: ifstream istrm;


istrm.open(“f2.txt”);


⒉关闭文件: outfile.close(); 文件流 outfile 被关闭,由它所标识的文件送入磁盘中。


例7:分析下列程序的输出结果:


void main()


{


ostream ostrm;


ostrm.open(“f1.txt”);


ostrm<<120<


ostrm<<310.85<


ostrm.close();


ifstream istrm(“f1.txt”);


int n;


double d;


istrm>>n>>d;


cout<


istrm.close();


}


2、 文本文件的读写操作:


 例8:把文本写入指定的文件中去。


#include


#include


#include


void main()


{


fstream outfile;


ourfile.open(“f2.txt”,ios::out);


if(!outfile)


{


cout<<”f2.dat cannt open.\n”;


abort();


}


outfile<<”this is a program.\n”;


outfile<<”this is a program.\n”;


outfile.close();


}


例9:从文本文件中读取文件信息:


#include


#include


#include


void main()


{


fstream infile;


infile.open(“f2.dat”,ios::in);


if(!infile)


{


cout<<”f2.dat cannt open.\n”;


abort();


}


char s[80];


while (!infile.eof())


{


infile.getline(s,sizeof(s));


cout<


}


inflie.close();


}


例 10 :使用 get() 和 put() 函数读写文本文件。


#include


#include


#include


#include


void main()


{


fstream outfile,infile;


outfile.open(“f3.dat”,ios::out);


if(!outfile)


{


cout<<”f3.dat cannt open.\n”;


abort();


}


char str[]=”this is a C++ program.”;


for(int I=0;I<=strlen(str);I++)


outfile.put(str[I]);


outfile.close();


inflie.open(“f3.dat”,ios::in);


if(!infile)


{


cout<<”f3.dat”,ios::in);


abort();


}


char ch;


while(infile.get(ch))


cout<


cout<


inflie.close();


}


例 11 : #include


#include


#include


void main()


{


fstream infile,outfile;


inflie.open(f2.dat”,ios::in);


if(!infile)


{


cout<<”f2.dat cannt open.\n”;


abort();


}


outfile.open(“f4.dat”,ios::out);


if(!outfile)


{


cout<<”f4.dat cannt open.\n”;


abort();


}


char ch;


while(infile.get(ch))


outfile.put();


infile.close();


outfile.close();


}


例 12 :二进制文件的读写操作:


#include


#include


#include


struct person


{


char name[20];


double height;


unsigned short age;


}


struct person people[40]={


“Wang”,1.65,25,


“Zhang”,1.74,24,


“Li”,1.89,21,


“Hang”,1.70,22};


void main()


{


fstream infile,outfile;


outfile.open(“f5.dat”,ios::out|ios::binary);


if(!outfile)


{


cout<<”f5.dat cannt open.\n”;


abort();


}


for(int I=0;I<4;I++)


outfile.write((char *)&people[I],sizeof(people[I]));


outfile.close();


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇二级C++输入输出流:键盘输入 下一篇二级C++输入输出流:字符串流

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目