-
Notifications
You must be signed in to change notification settings - Fork 37
/
7-8.c
79 lines (68 loc) · 1.71 KB
/
7-8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Exercise 7-8. 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.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXHEADER 6 /* maximum header size */
#define MAXFOOTER 3 /* maximum footer size */
#define MAXLINE 80 /* maximum characters per line */
#define MAXPAGE 60 /* maximum lines per page */
/* globals */
const char *progName;
/* functions */
FILE* loadFile(char *);
int printHeader(char *, int);
void printFile(FILE *, char *);
FILE* loadFile(char *fileName)
{
FILE *fp;
if (!(fp = fopen(fileName, "r"))) {
fprintf(stderr, "%s: can't open %s\n", progName, fileName);
exit(EXIT_FAILURE);
}
return fp;
}
int printHeader(char *fileName, int pageNo)
{
int ln = 5; /* length of the lines bellow */
printf("\n************************\n");
printf("File name: %s\n", fileName);
printf("Page: %i\n", pageNo);
printf("************************\n");
while (ln++ < MAXHEADER)
fprintf(stdout, "\n");
return ln;
}
void printFile(FILE *file, char *fileName)
{
char line[MAXLINE];
int lineNo,pageNo;
lineNo = pageNo = 1;
while (fgets(line, MAXLINE, file)) {
if (lineNo == 1) {
fprintf(stdout, "\f");
lineNo = printHeader(fileName, pageNo++);
}
fputs(line, stdout);
if (++lineNo > MAXPAGE - MAXFOOTER)
lineNo = 1;
}
fprintf(stdout, "\f");
}
int main(int argc, char *argv[])
{
FILE *fp;
progName = *argv;
if (argc == 1) /* standard input */
printFile(stdin, "standard input");
else
while (--argc > 0) {
fp = loadFile(*++argv);
printFile(fp, *argv);
fclose(fp);
}
exit(EXIT_SUCCESS);
}