您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> C十十 >> 辅导 >> 正文    
  二级C++输入输出流:磁盘文件的输入和输出 【注册男护士专用博客】          

二级C++输入输出流:磁盘文件的输入和输出

www.nanhushi.com     佚名   不详 

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<<endl;

ostrm<<310.85<<endl;

ostrm.close();

ifstream istrm(“f1.txt”);

int n;

double d;

istrm>>n>>d;

cout<<n<<”,”<<d<<endl;

istrm.close();

}

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

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

#include <iostream.h>

#include<fstream.h>

#include<stdlib.h>

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<iostream.h>

#include <fstream.h>

#include<stdlib.h>

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<<s<<endl;

}

inflie.close();

}

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

#include <iostream.h>

#include<fstream.h>

#include<stdlib.h>

#include<string.h>

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<<ch;

cout<<endl;

inflie.close();

}

例 11 : #include <iostream.h>

#include<fstream.h>

#include<stdlib.h>

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<iostream.h>

#include<fstream.h>

#include<stdlib.h>

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();

infile.open(“f5.dat”,ios::in|ios::binary”);

if(!infile)

{

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

abort();

}

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

{

infile.read((char *)&people[I],sizeof(people[I]));

cout<<people[I].name<<'\t'<<people[I].height<<'\t'<<people[I].age<<endl;

}

infile.close();

}

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目