0

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);
}
}
Gomathi
  • 973
  • 7
  • 17
  • 37

2 Answers2

1

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++

Community
  • 1
  • 1
Adeel Ahmed
  • 1,591
  • 8
  • 10
  • I already viewed that answer. It only clears the previous two lines. My coding clears even an entire screen! – Gomathi Dec 20 '12 at 10:22
  • Thanks. But clearing isn't a matter, which I can already do using my code. I want it similar to a man page. Please read my question again. – Gomathi Dec 20 '12 at 10:35
1
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.

Barmar
  • 741,623
  • 53
  • 500
  • 612