简单写了个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 1024
#define ROWS 15
char **pline=NULL;
int lines = 0;
int row = 0;
void _open(), _pageup(), _pagedown(), _lineup(), _linedown(), _homepage(), _endpage(), _quit(), _close(), _print();
void (*command[])() = {_quit, _open, _pageup, _pagedown, _lineup, _linedown, _homepage, _endpage};
main()
{
char cmd;
while(1)
{
printf("\n请选择[1.打开, 2.上页, 3.下页, 4.上行, 5.下行, 6.首页, 7.尾页, 0.关闭]: _\b");
while (((cmd=getch()) < '0') || (cmd > '7')) ;
(*command[cmd-'0'])();
}
}
void _close()
{
int i;
for(i=0; i<lines; i++)
free(pline[i]);
free(pline);
pline = NULL;
lines = 0;
row = 0;
}
void _quit()
{
_close();
exit(0);
}
void _print()
{
system("cls");
printf("\n");
int i;
for (i=row; ((i-row)<ROWS) && (i<lines); i++)
printf(pline[i]);
}
void _open()
{
char fname[FILENAME_MAX]="Readme.txt";
printf("输入文件名(Readme.txt):");
//gets(fname);
FILE *fp;
if ((fp=fopen(fname,"r")) == NULL)
{
puts("打开文件失败");
return;
}
_close();
char buf[BUFFERSIZE];
while (fgets(buf, BUFFERSIZE, fp) != NULL)
{
pline = (char **)realloc(pline, (++lines)*sizeof(char *));
pline[lines-1] = (char *)malloc(strlen(buf)+1);
strcpy(pline[lines-1], buf);
}
fclose(fp);
_print();
}
void _pageup()
{
row = (row>=ROWS ? row-ROWS : 0);
_print();
}
void _pagedown()
{
row += ((row+ROWS)<lines ? ROWS : 0);
_print();
}
void _lineup()
{
if (pline == NULL)
return;
row -= (row>0 ? 1 : 0);
printf("\n\n[%d] %s", row+1, pline[row]);
}
void _linedown()
{
if (pline == NULL)
return;
row += (row<(lines-1) ? 1 : 0);
printf("\n\n[%d] %s", row+1, pline[row]);
}
void _homepage()
{
row = 0;
_print();
}
void _endpage()
{
row = (lines>ROWS ? lines-ROWS : 0);
_print();
}
[此贴子已经被作者于2016-12-22 20:04编辑过]