设计一文件阅读器, 可以一次一屏(20或22行)显示文件内容, 每次显示完一屏内容后, 提示使用者键入一控制字符以控制屏幕翻滚。如字符'n'显示下一屏, 字符'
#include <iostream>#include <fstream>
#include <string>
#include <vector>
using namespace std;
class TxtReader
{
private:
ifstream txtfile;
static const int linesize;
static const int rowsize;
vector<fstream::streampos>screenpos;
public:
TxtReader(string name);
void LastScreen();
void NextScreen();
void ShowContent();
~TxtReader();
};
const int TxtReader::linesize = 22; //一屏22行
const int TxtReader::rowsize = 70; //一行70个字符
TxtReader::TxtReader(string name)
{
txtfile.open(name.c_str());;
if(!txtfile)
{
cout << "Open file failed!" << endl;
return;
}
screenpos.push_back(txtfile.tellg());
ShowContent();
}
TxtReader::~TxtReader()
{
txtfile.close();
}
void TxtReader::ShowContent()
{
char tmp[rowsize+1];
int line = 0;
while((!txtfile.eof())&&(line!=linesize))
{
txtfile.getline(tmp, rowsize+1);
line += 1;
cout << tmp << endl;
}
}
void TxtReader::NextScreen()
{
if(txtfile.eof())
return;
screenpos.push_back(txtfile.tellg());
ShowContent();
}
void TxtReader::LastScreen()
{
if(screenpos.size() == 1)
return;
txtfile.seekg(screenpos.back());
screenpos.pop_back();
ShowContent();
}
int main()
{
cout << "Input the file name to open:" << endl;
string filename;
cin >> filename;
TxtReader reader(filename);
char control;
while(cin >> control)
{
if(control == 'n')
reader.NextScreen();
if(control == 'p')
reader.LastScreen();
if(control == 'q')
break;
}
}
这个运行不了 求帮助