【发布时间】:2013-12-27 16:30:59
【问题描述】:
我想在屏幕上显示 20 行文本文件的内容。 用户按“n”显示下 20 行,按“p”显示前 20 行。 我不知道为什么它不起作用。
这是我的代码:
#include<fstream.h>
#include<process.h>
#include<conio.h>
void main()
{
clrscr();
char *s,key;
int pos;
fstream f;
f.open("menu.cpp",ios::in);
while(1)
{
key=getch();
switch(key)
{
case 'n':
//read more 20 lines
clrscr();
for(int i=1;i<=25;i++)
{
f.getline(s,100);
pos=f.tellg();
cout<<s<<endl;
}
break;
case 'p': //read previous 20 lines
clrscr();
f.seekg(-pos);
for(int i=1;i<=25;i++)
{
f.getline(s,100);
cout<<s<<endl;
}
break;
case 'e':
clrscr();
cout<<"exit";
exit(0);
}
}
}
【问题讨论】:
-
您可能还应该发布它实际在做什么,这样人们就不必编译和运行程序。和/或您收到的任何错误消息。
-
哦..我忘了提...接下来的 20 行显示正确,但是当我按“p”时,前 20 行没有显示,然后当我按“e”时程序也不退出。