输入输出流例子.docx

上传人:b****6 文档编号:8696333 上传时间:2023-02-01 格式:DOCX 页数:9 大小:16.78KB
下载 相关 举报
输入输出流例子.docx_第1页
第1页 / 共9页
输入输出流例子.docx_第2页
第2页 / 共9页
输入输出流例子.docx_第3页
第3页 / 共9页
输入输出流例子.docx_第4页
第4页 / 共9页
输入输出流例子.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

输入输出流例子.docx

《输入输出流例子.docx》由会员分享,可在线阅读,更多相关《输入输出流例子.docx(9页珍藏版)》请在冰豆网上搜索。

输入输出流例子.docx

输入输出流例子

例14.16

用友元函数实现复数类对象插入和提取运算符重载函数

#include

usingnamespacestd;

classComplex

{

doubleReal,Image;

public:

Complex(doubler=0,doublei=0)

{

Real=r;Image=i;

}

friendistream&operator>>(istream&,Complex&);

friendostream&operator<<(ostream&,Complex&);

};

istream&operator>>(istream&in,Complex&c)友元函数

{注意参数和返回值

in>>c.Real>>c.Image;

returnin;

}

ostream&operator<<(ostream&out,Complex&c)友元函数

{注意参数和返回值

out<

if(c.Image>0)out<<'+'<

elseif(c.Image<0)out<

out<

returnout;

}

intmain()

{

Complexc1(1,2),c2;

cout<

cout<<"Pleaseinputc1&c2:

\n";

cin>>c1>>c2;

//解释为operator>>(operator>>(cin,c1),c2);

cout<

//解释为operator<<(operator<<(cout,c1),c2);

return0;

}

程序的运行状况如下:

1+2i

Pleaseinputc1&c2:

58<回车>//输入

7-3<回车>//输入

5+8i

7-3i

返回ppt讲稿

 

例14.22编一个程序用于复制文本文件

#include

#include

#include

usingnamespacestd;

intmain()

{

charinfilename[40],outfilename[40],ch;

cout<<"Pleaseinputaninputfilename:

";

cin>>infilename;

cout<<"Pleaseinputanoutputfilename:

";

cin>>outfilename;

fstreaminfile(infilename,ios:

:

in);

if(!

infile)

{

cout<<"Cannotopeninputfile:

"

<

exit

(1);

}

fstreamoutfile(outfilename,ios:

:

out);

if(!

outfile)

{

cout<<"Cannotopenoutputfile:

"

<

exit

(2);

}

while(infile.get(ch))

outfile<

infile.close();

outfile.close();

return0;

}

返回ppt讲稿

 

例14.23编一个程序从一个文本文件source.txt中读入若干整数,用选择法将这些数据排成升序,将排序后的结果写入另一个文件文本文件target.txt中。

注意两个文件均在d盘的data文件夹中。

 

#include

#include

#include

usingnamespacestd;

voidsort(int*a,intn)//一般的选择法排序函数

{

inti,j,p,t;

for(i=0;i

{

p=i;

for(j=i+1;j

if(a[j]

p=j;

if(p!

=i)

{t=a[i];a[i]=a[p];a[p]=t;}

}

}

intmain()

{

inta[100],i,n;

fstreamin,out;//若路径缺省,指当前目录

in.open("d:

\\data\\source.txt",ios:

:

in);

if(!

in)

{

cout<<"Cannotopensource.txt!

"<

exit

(1);

}

out.open("d:

\\data\\target.txt",ios:

:

out);

if(!

out)

{

cout<<"Cannotopentarget.txt!

"<

exit

(2);

}

i=0;

while(in>>a[i])i++;//循环结束后,i是整数的个数

sort(a,i);

n=i;

for(i=0;i

out<

in.close();

out.close();

return0;

}

在程序运行前,先准备好输入数据文件source.txt,放入d盘的data文件夹中,内容可以如下:

231045

338920

45678883

7232-2

0-1

 

程序运行结束后,查看d盘的data文件夹中的结果文件target.txt内容是否正确。

返回ppt讲稿

 

例14.25编写一个程序对二进制文件进行读写。

本程序的功能是,从键盘输入若干学生的信息,写入二进制文件,再从该二进制文件中读出学生的信息,输出到屏幕上。

#include

#include

#include

#include

structstudent//定义一个结构体类型

{

charname[10];//姓名

charid[10];//学号

intscore;//分数

};

#defineLENsizeof(structstudent)

intmain()

{

studentst;

fstreamfile("stud.dat",ios:

:

out|ios:

:

binary);

//以二进制方式打开输出文件

if(!

file)

{

cout<<"Cannotopenoutputfile:

stud.dat"<

exit

(1);

}

cin>>st.name;

while(strcmp(st.name,"#")!

=0)

//循环输入时,以输入姓名为“#”结束

{

cin>>st.id>>st.score;

//循环从键盘输入学生信息

file.write((char*)&st,LEN);

//一次写出LEN字节的内存数据

cin>>st.name;

}

file.close();

//关闭与file关联的文件,以便后面重复使用file对象

studentsts[100];

inti=0,j;

file.open("stud.dat",ios:

:

in|ios:

:

binary);

//重复使用file对象

if(!

file)

{

cout<<"Cannotopeninputfile:

stud.dat"<

exit

(2);

}

while(file.read((char*)(sts+i),LEN))

//一次读入LEN字节的数据,存入内存指定地址

i++;

for(j=0;j

cout<

<<'\t'<

file.close();

return0;

}

程序的一次运行状况如下:

wss010180

tyy010290

czz010385

#

wss010180

tyy010290

czz010385

返回ppt讲稿

 

例14.26读入文本文件data.txt中的数据,

写入二进制文件data.bin中。

然后再从二进制文件中读入全部整数值,输出到屏幕上。

#include

#include

#include

usingnamespacestd;

intmain()

{

inta[100],n=0;

ifstreaminfile("data.txt");

if(!

infile)

{

cout<<"Cannotopeninputfile:

data.txt"<

exit

(1);

}

while(!

infile.eof())//eof()函数的功能?

infile>>a[n++];//读入到数组中

infile.close();

ofstreamoutfile("data.bin",ios:

:

out|ios:

:

binary);

if(!

outfile)

{

cout<<"Cannotopenoutputfile:

data.dat"<

exit

(2);

}

outfile.write((char*)&n,sizeof(int));//写出元素个数

outfile.write((char*)a,n*sizeof(int));//写出整个数组

outfile.close();

intb[100],m=0,i;

infile.open("data.bin",ios:

:

in|ios:

:

binary);

//重复使用infile对象

infile.read((char*)&m,sizeof(int));//读入元素个数

infile.read((char*)b,m*sizeof(int));//读入整个数组

infile.close();

for(i=0;i

cout<

cout<

return0;

}

返回ppt讲稿

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1