Pergunta

I want to read a text file and display it. But I need to print it on terminal, similar to the man page (linux). That is, when scrolled up, it shouldn't go beyond the first line and scrolling down shouldn't go beyond the last line. I have to program it only in C. I shouldn't use any tools. My current coding for clearing a terminal alone is,

#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);
}

Kindly guide me. Thanks in advance.

UPDATED:

main()
{
FILE *ffp;
char c;


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

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

Solução

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

more and less are the programs that implement scrolling through a file or pipe a screenful at a time.

Outras dicas

try something like this,,

#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;
}

SOURCE: Clearing output of a terminal program Linux C/C++

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top