Exercise 7.8 - Print Pages to Files

Question

Write a program to print a set of files, starting each new one on a new page, with a title and a running page count for each file.

/*
 * Write a program to print a set of files, starting each new one on a new page,
 * with a title and a running page count for each file.
 *
 */

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

#define LINELENGTH 80
#define LINESPERPAGE 10

void printpages(FILE *, FILE *);

int main(int argc, char *argv[]) {
    FILE *fp;
    char *prog = argv[0];
    int linecount = 0;

    if (argc == 1) /* no args; copy standard input */
    {
        fprintf(stderr, "No files given");
        printpages(stdin, stdout);
    } else
        while (--argc > 0)
            if ((fp = fopen(*++argv, "r")) == NULL) {
                fprintf(stderr, "%s: can't open %s\n", prog, *argv);
                exit(1);
            } else {
                fprintf(stdout, "\n\n\t\t\tFile: %s\n\n", *argv);
                printpages(fp, stdout);
                fclose(fp);
            }

    if (ferror(stdout)) {
        fprintf(stderr, "%s: error writing stdout\n", prog);
        exit(2);
    }
    exit(0);
}

/* filecopy: copy file ifp to file ofp */

void printpages(FILE *ifp, FILE *ofp) {
    int c;
    int line = 0;
    int pg = 1;

    while ((c = getc(ifp)) != EOF) {
        putc(c, ofp);
        if (c == '\n') {
            line = line + 1;
            if (line == LINESPERPAGE) {
                fprintf(stdout, "\n\t\t\tPage %d End.\n\n", pg);
                pg = pg + 1;
                line = 0;
            }
        }
    }
}

Explanation

This takes a file as argument, and prints the file into number of pages, with Page # End as a de-mark at the end of the page.

$ ./ex_7.8 README.md


                        File: README.md

# https://learntosolveit.com

[https://learntosolveit.com](https://learntosolveit.com) is a website to learn C programming using K&R book. It uses modern tools, and is designed to be used along with the book.

To practice the exercises, you can use the online compilers like

* [https://www.tutorialspoint.com/compile_c_online.php](https://www.tutorialspoint.com/compile_c_online.php)
* [https://replit.com/](https://replit.com/)
* [https://www.jdoodle.com/c-online-compiler](https://www.jdoodle.com/c-online-compiler)
* [https://www.onlinegdb.com/online_c_compiler](https://www.onlinegdb.com/online_c_compiler)

                        Page 1 End.

* [https://www.codechef.com/ide](https://www.codechef.com/ide)
* [https://www.programiz.com/c-programming/online-compiler/](https://www.programiz.com/c-programming/online-compiler/).

I recommend [https://exercism.org](https://exercism.org) as the platform to
learn programming, including C, and practice with a community of intrinsically
motivated developers.

### Reference Books

* C Programming Language by Kernighan and Ritchie.

                        Page 2 End.



[![Netlify Status](https://api.netlify.com/api/v1/badges/27a766e4-762c-420f-92e2-f35441c79f63/deploy-status)](https://app.netlify.com/sites/learntosolveit/deploys)
[![Documentation Status](https://readthedocs.org/projects/learntosolveit/badge/?version=latest)](http://www.learntosolveit.com/?badge=latest)


## Author

* Senthil Kumaran
* Email: [senthil@uthcode.com](mailto:senthil@uthcode.com)

                        Page 3 End.

* Blog: [https://senthil.learntosolveit.com](https://senthil.learntosolveit.com)