c++题目 设计一个文件阅读器

作者&投稿:辉勤 (若有异议请与网页底部的电邮联系)
c++题目 编写一个TXT文件阅读器~

#include
#include
#include
#include using namespace std;class TxtReader
{
private:
ifstream txtfile;
static const int linesize;
static const int rowsize;
vector 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;
}
}

可以编一个函数用来进行连乘,如第一次调用进行1乘1,2乘调用时再乘以,2,第3次调用时再乘以3,以此规律进行下去

getline()函数,很好用的,可以按行读
//Example
/*21chenxb's work
*2010-4-27
*VC++6.0
*Reader.cpp
*/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define Max_Line 20//一屏最在行数
#define Max_Screen 20//最多屏数
struct perscreen
{
string sr[Max_Line];
}Reader[Max_Screen];
int t;//总读取屏数
void Print(perscreen *Reader,int i);

void main()
{
char* filename;
ifstream ifs;
cout<<"请输入要阅读的文件名:"<<endl;
filename="Test.txt";
//cin>>filename;//需要输入文件名阅读的时候,使用这句
ifs.open(filename);
if (!ifs)
{
cout<<"文件打开失败!"<<endl;
}
int i=0;
string s;
while (getline(ifs,s))
{
t=i/Max_Line;
Reader[t].sr[i%Max_Line]=s;
i++;
}
Print(Reader,0);
}
void Print(perscreen *Reader,int i)
{
if (i<0)//小于0,返回第一屏
{
i=0;
}
if (i>t)//超过已有屏数,返回最后一屏
{
i=t;
}
cout<<"--------------------------------------------------------当前第"<<i+1<<"屏"<<endl;
for (int j=0;j<Max_Line;j++)
{
cout<<Reader[i].sr[j]<<endl;
}
char choose;
cout<<"-----------------------------------------------------------"<<endl;
cout<<"n:下一屏"<<"u:上一屏"<<"q:退出"<<endl;
cin>>choose;
system("cls");
switch (choose)
{
case 'n': Print(Reader,i+1);break;
case 'u': Print(Reader,i-1);break;
case 'q': return;
}
}

我们可以免费提供一份针对于你的题目的免费的初学者代码
有进一步需求,可以我们联系,
给我留一个你的问题和Email,
绝对救急,请到个人资料里查看OICQ,
帮你顺利毕业,


连州市15791849755: c++题目 设计一个文件阅读器 -
赫软肉蔻: getline()函数,很好用的,可以按行读 //Example/*21chenxb's work*2010-4-27*VC++6.0*Reader.cpp*/#include <iostream>#include <string>#include <fstream> using namespace std;#define Max_Line 20//一屏最在行数#define Max_Screen 20//...

连州市15791849755: c++设计一文件阅读器, 可以一次一屏(20或22行)显示文件内容, 每次显示完一屏内容后, 提示? -
赫软肉蔻: 1. 统计一源程序语句数、行数、字符数、类及函数的个数. 2. 将源程序每行前加上行号并删除其所有注释后生成一打印文件. 注意C++语言程序注释形式有://注释内容 和 /* 注释内容 */ 两种形式.3. 编一查找给定字符串程序,要求输出给定字...

连州市15791849755: c++题目 编写一个TXT文件阅读器 -
赫软肉蔻: #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<fstrem::streampos> screenpos; public: TxtReader(...

连州市15791849755: 如何用C++开发一个小说阅读器 -
赫软肉蔻: 在退出按钮旁边有个搜索栏,输入你想要搜索的作品的名字,然后打回车,会自动蹦出一个对话框,上面有阅读器搜索到的与你想要的作品相关的下载网站,选择一个,点击“下载”,就可以了 还可以点击工具栏最左边的“新增”按钮,在小说名框里输入小说名,在目录页地址框里粘贴上你所要看的小说目录页的地址.记住,一定是目录页.然后选择分类,点击确定.其实,你自己琢磨琢磨就会用咯

连州市15791849755: C++如何编写TXT阅读器 -
赫软肉蔻: #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<fstrem::streampos> screenpos; public: TxtReader(...

连州市15791849755: vc++6.0编程题目 -
赫软肉蔻: 12,设计思路:命令行接受文件名. 打开文件后,先加载前20行,根据换行字符判断移动的位置,使用fseek移动文件指针;移动完一次后,接受标准输入,根据字符进行判断要执行的后续动作,使用fseek移动到影响的位置,将对应的数据打印...

连州市15791849755: C++书籍借阅系统源代码
赫软肉蔻: http://wenku.baidu.com/search?word=C%2B%2B%CA%E9%BC%AE%BD%E8%D4%C4%CF%B5%CD%B3%D4%B4%B4%FA%C2%EB&lm=0&od=0&fr=top_home 链接里面有响应的源代码

连州市15791849755: c#课程设计,求代码跟思路.题目如下: Windows窗体应用程序设计题目: 编写程序,实现一个 -
赫软肉蔻: 感觉就是字符串的处理.openfile组件的使用.最后一个显示.就是form的属性.这几个都是最直接的修改属性.修改字符串.基本没有什么可说的.不建议你直接找人要代码.这个你查一下相应的控件属性.都是基本的东西.

连州市15791849755: c++pdf开发 -
赫软肉蔻: 每一页面占用的空间一样,只要知道文件的开始存储指针就OK了.后面就是按页为单位为检索页面嘛.主要还是要知道文件的存储方式,

连州市15791849755: c++关于文本文件读写的一个编程题 -
赫软肉蔻: 文件的输出与输入 头文件#include<stdio> …… int main() { freopen("1.txt","r",stdin)//读取在这个文件的目录下名为:1.txt的文件 freopen("1.txt","w",stdout)//在这个文件的目录下写名为:1.txt的文件 …… 补充:如果两个文件的文件名重复,新文件将会覆盖旧文件

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网