C++实验报告流类库与输入输出
- 格式:pdf
- 大小:44.94 KB
- 文档页数:2
C++实验报告流类库与输入/输出
实验八流类库与输入/输出(2学时)
一.实验目的:
1.熟悉流类库中常用的类及其成员函数使用方法;
2.学习标准输入输出及格式控制;
3.学习对文件的应用方法(二进制文件,文本文件)
二.实验内容与步骤:
1.实验题目:(2):编写程序,用二进制方式打开指定的一个文件,在每一行前加行号.(提示:使用void main(int argc,char*argv[])函数中的参数传递操作的文件名,定义ofstream的对象对文件进行操作.使用getline成员函数读入数据,使用cout输出字符到文件.)
2.程序清单:
#include<fstream>
#include<strstream>
#include<cstdlib>
using namespace std;
void main(){
strstream file;
ifstream in("f1.txt",ios::binary);
file<<in.rdbuf();
ofstream out("f1.txt",ios::binary);
const int s=255;
char l[s];
for(int i=1;file.getline(l,s);i++)
out<<i<<"."<<l<<endl; }
3.测试结果如下:
三.实验小结:
1,重要的输出流:ostream,ofstream,ostringstream;
2,重要的输入流:istream,ifstream,istringstream.。