|
1 | 1 | /*
|
2 | 2 | * Exercise 7-8. Write a program to print a set of files, starting each new one
|
3 | 3 | * on a new page, with a title and a running page count for each file.
|
| 4 | + * |
4 | 5 | * By Faisal Saadatmand
|
5 | 6 | */
|
6 | 7 |
|
7 | 8 | #include <stdio.h>
|
8 | 9 | #include <stdlib.h>
|
9 | 10 |
|
10 |
| -#define LINE 81 /* characters per line */ |
11 |
| -#define PAGEBREAK 3840 /* characters per page ~ 500 word/page */ |
| 11 | +#define MAXHEADER 6 /* maximum header size */ |
| 12 | +#define MAXFOOTER 3 /* maximum footer size */ |
| 13 | +#define MAXLINE 80 /* maximum characters per line */ |
| 14 | +#define MAXPAGE 60 /* maximum lines per page */ |
| 15 | + |
| 16 | +/* globals */ |
| 17 | +const char *progName; |
12 | 18 |
|
13 | 19 | /* functions */
|
14 |
| -int pagecount(FILE *); |
15 |
| -void printcover(char *, int); |
| 20 | +FILE* loadFile(char *); |
| 21 | +int printHeader(char *, int); |
| 22 | +void printFile(FILE *, char *); |
16 | 23 |
|
17 |
| -int pagecount(FILE *f) |
| 24 | +FILE* loadFile(char *fileName) |
18 | 25 | {
|
19 |
| - int count = 0; |
20 |
| - |
21 |
| - while (getc(f) != EOF) |
22 |
| - count++; |
23 |
| - return (count < PAGEBREAK) ? 1 : count / PAGEBREAK; |
| 26 | + FILE *fp; |
| 27 | + if (!(fp = fopen(fileName, "r"))) { |
| 28 | + fprintf(stderr, "%s: can't open %s\n", progName, fileName); |
| 29 | + exit(EXIT_FAILURE); |
| 30 | + } |
| 31 | + return fp; |
24 | 32 | }
|
25 | 33 |
|
26 |
| -void printcover(char *s, int n) |
| 34 | +int printHeader(char *fileName, int pageNo) |
27 | 35 | {
|
28 |
| - int i; |
| 36 | + int ln = 5; /* length of the lines bellow */ |
29 | 37 |
|
| 38 | + printf("\n************************\n"); |
| 39 | + printf("File name: %s\n", fileName); |
| 40 | + printf("Page: %i\n", pageNo); |
30 | 41 | printf("************************\n");
|
31 |
| - printf("File name: %s\n", s); |
32 |
| - printf("Page count: %i\n", n); |
33 |
| - printf("************************\n"); |
| 42 | + while (ln++ < MAXHEADER) |
| 43 | + fprintf(stdout, "\n"); |
| 44 | + return ln; |
| 45 | +} |
34 | 46 |
|
35 |
| - for (i = 4; i <= (PAGEBREAK / LINE); ++i) /* i = 4 to skip above lines */ |
36 |
| - printf("\n"); |
| 47 | +void printFile(FILE *file, char *fileName) |
| 48 | +{ |
| 49 | + char line[MAXLINE]; |
| 50 | + int lineNo,pageNo; |
| 51 | + |
| 52 | + lineNo = pageNo = 1; |
| 53 | + while (fgets(line, MAXLINE, file)) { |
| 54 | + if (lineNo == 1) { |
| 55 | + fprintf(stdout, "\f"); |
| 56 | + lineNo = printHeader(fileName, pageNo++); |
| 57 | + } |
| 58 | + fputs(line, stdout); |
| 59 | + if (++lineNo > MAXPAGE - MAXFOOTER) |
| 60 | + lineNo = 1; |
| 61 | + } |
| 62 | + fprintf(stdout, "\f"); |
37 | 63 | }
|
38 | 64 |
|
39 | 65 | int main(int argc, char *argv[])
|
40 | 66 | {
|
41 | 67 | FILE *fp;
|
42 |
| - char *prog = argv[0]; |
43 |
| - int pages, c; |
44 |
| - |
45 |
| - while (--argc > 0) |
46 |
| - if ((fp = fopen(*++argv, "r")) == NULL) { |
47 |
| - fprintf(stderr, "%s: can't open %s\n", prog, *argv); |
48 |
| - exit(EXIT_FAILURE); |
49 |
| - } else { |
50 |
| - pages = pagecount(fp); |
51 |
| - rewind(fp); /* rewind input stream */ |
52 |
| - printcover(*argv, pages); |
53 |
| - while ((c = getc(fp)) != EOF) /* print file */ |
54 |
| - fprintf(stdout, "%c", c); |
| 68 | + |
| 69 | + progName = *argv; |
| 70 | + if (argc == 1) /* standard input */ |
| 71 | + printFile(stdin, "standard input"); |
| 72 | + else |
| 73 | + while (--argc > 0) { |
| 74 | + fp = loadFile(*++argv); |
| 75 | + printFile(fp, *argv); |
55 | 76 | fclose(fp);
|
56 | 77 | }
|
57 | 78 | exit(EXIT_SUCCESS);
|
|
0 commit comments