![]() |
#2
小白兰2012-09-06 16:50
这个程序代码自己改了下,还是运行不了啊,求大神!!!!
![]() ![]() #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); 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; } } |




下面是题目:
设计一文件阅读器, 可以一次一屏(20或22行)显示文件内容, 每次显示完一屏内容后, 提示使用者键入一控制字符以控制屏幕翻滚。如字符'n'显示下一屏, 字符'p'返回上一屏, 字符'q'结束阅读。
在这谢谢各位了!!!!
[ 本帖最后由 小白兰 于 2012-9-6 16:42 编辑 ]