0

我想读取一个文本文件并显示它。但我需要在终端上打印它,类似于手册页(linux)。也就是说,向上滚动时不应超出第一行,向下滚动不应超出最后一行。我只能用 C 语言编写它。我不应该使用任何工具。我目前单独清除终端的编码是,

#include<stdio.h>

main()
{
printf("\033[2J");
printf("\033[0;0f");
FILE *ffp;
char c;

ffp=fopen("help.txt","r");
while((c=getc(ffp))!=EOF)
    printf("%c",c);
}

请指导我。提前致谢。

更新:

main()
{
FILE *ffp;
char c;


ffp=fopen("help.txt","r");

FILE *less = popen("less", "w");
while ((c = getc(ffp)) != EOF) {
  fputc(c, less);
}
}
4

2 回答 2

1

尝试这样的事情,,

#include <stdio.h>

int
main ()
{
    fputs("output1\n",stdout);
    fputs("output2\n",stdout);
    fputs("\033[A\033[2K\033[A\033[2K",stdout);
    rewind(stdout);
    ftruncate(1,0); /* you probably want this as well */
    fputs("output3\n",stdout);
    fputs("output4\n",stdout);
    return 0;
}

来源: 清除终端程序 Linux C/C++ 的输出

于 2012-12-20T10:20:42.237 回答
1
FILE *less = popen("less", "w");
while ((c = getc(ffp)) != EOF) {
  fputc(c, less);
}

more并且less是一次实现滚动文件或管道的程序。

于 2012-12-20T10:25:10.170 回答