LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C: How can i print out the content of a file in pages? (http://www.linuxquestions.org/questions/programming-9/c-how-can-i-print-out-the-content-of-a-file-in-pages-162443/)

akin81 03-25-2004 08:29 PM

C: How can i print out the content of a file in pages?
 
I am writing a C program for Linux.
If the content of a file is too large for one terminal screen, how can i print the content in pages?
Just like when we use the "man" function.

aluser 03-25-2004 08:46 PM

As a user I'd be most happy if you piped the output into $PAGER, or 'more' by default. This would look like

Code:

#include <stdlib.h>
#include <stdio.h>

#define NLINES 500

int main()
{
        char* pager = getenv("PAGER");
        FILE* pager_stream;
        int i;
        if (pager == NULL) {
                pager = "more";
        }
        pager_stream = popen(pager, "w");
        if (pager_stream == NULL)
                err(EXIT_FAILURE, "popen");

        for (i = 0; i < NLINES; ++i) {
                fprintf(pager_stream, "Hello, World %d\n", i);
        }

        pclose(pager_stream);

        return 0;
}



All times are GMT -5. The time now is 06:06 PM.