C++编程 浏览文本文件

来源:百度知道 编辑:UC知道 时间:2024/05/30 01:34:08
实现一屏一屏地浏览文本文件。程序开始时要求输入文本文件名,确认后输出该文本的内容到屏幕上,如果文本内容超过一屏可显示的范围,则只显示一部分。在输入‘N’后显示下面一部分内容,输入‘P’后显示上面一部分内容,输入‘E’后退出程序。

#include<iostream.h>//C++浏览文本文件
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include <conio.h> //system(cls)清屏
void main()
{
int r; char c;
fstream rs;
char fn[15],buf[100];
cout<<"Input the rs'name:";
cin>>fn; //输入文件名
rs.open(fn,ios::nocreate|ios::in);
//针对文件后缀为(.txt .h .cpp .pas等)文件
if(!rs)
{
cout<<"The rs you wanted open does NOT exist.";
abort(); //#include<stdlib.h>
}
c='n';
while(c!='e'&&c!='E')
{
system(cls); //清屏
r=0;
if (c=='n'||c=='N')
while(!rs.eof() && r<23)
{
rs.getline(buf,100); //读取100个字符到内存buf中
cout<<buf<<endl; //输出一行元素
r++; //下一行,一页23行
}
if (c=='e'||c