C++实验7 I/O流基础

本文档详细介绍了C++流类库的实验教程,包括流类库中常用类及成员函数的使用方法,标准输入输出及格式控制的学习,以及对文件应用的实践。通过具体代码示例,深入解析了如何控制输出格式,设置宽度、精度和填充字符,以及如何读取和写入二进制文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、实验目的和要求

 1.熟悉流类库中常用的类及其成员函数的用法。

2.学习标准的输入输出及格式控制。

3.学习对文件的应用方法

二、实验环境(软、硬件及条件)

一台安装有Visual C++ 6.0的计算机

三、实验步骤

1.写出下列程序的运行结果。

#include "iostream.h"

 

void showflags(long f)

{

long i = 0x8000;

for(; i; i=i>>1)

{

if(i&f)

cout<<"1";

else

cout<<"0";

}

cout<<endl;

}

 

void main()

{

showflags(cout.flags());

cout<<"x_width="<<cout.width()<<endl;

cout<<"x_fill="<<cout.fill()<<endl;

cout<<"x_precision="<<cout.precision()<<endl;

cout<<123<<"    "<<123.45678<<endl;

cout<<"-----------------------------"<<endl;

cout<<"* * * x_width=10,x_fill= ,x_precision=4 * * *"<<endl;

cout.width(10);

cout.precision(4);

cout<<123<<"    "<<123.45678<<"    "<<234.567<<endl;

cout<<"x_width="<<cout.width()<<endl;

cout<<"x_fill="<<cout.fill()<<endl;

cout<<"x_precision="<<cout.precision()<<endl;

cout<<"-----------------------------"<<endl;

cout<<"* * * x_width=10,x_file=&,x_precision=4 * * *"<<endl;

cout.fill('&');

cout.width(10);

cout<<123<<"    "<<123.45678<<endl;

cout.setf(ios::left);

cout.width(10);

cout<<123<<"    "<<123.45678<<endl;

cout<<"x_width="<<cout.width()<<endl;

cout<<"x_fill="<<cout.fill()<<endl;

cout<<"x_precision="<<cout.precision()<<endl;

showflags(cout.flags());

cout.setf(ios::right|ios::unitbuf);

showflags(cout.flags());

}

 

(提示:在x_flags变量中left=0x0002,right=0x0004,unitbuf=0x2000)

 

 

 

2、观察程序Lab.cpp的输出结果,学习对输出格式的控制方法;尝试更改输出语句中的参数,以加深对输出格式的理解。程序Lab.cpp代码如下:

#include <iostream.h>

#include <iomanip.h>

void main()

{

double values[]={1.23,35.36,1653.7,54358.324354};

char *names[]={"Zoot","Jimmy","A1","Yehengzhou"};

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

{

cout.width(8);

if(i==1) cout.fill('*');

cout<<setprecision(10)<<values[i]<<endl;

}

cout.fill(' ');

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

cout<<setiosflags(ios::left)<<setw(12)<<names[i]

<<resetiosflags(ios::left)<<setw(12)<<values[i]<<endl;

}

 

 

 

3、编写程序,用二进制方式打开指定的一个文本文件,在每一行前加上行号。

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值