Skip to content

Commit bfb23a6

Browse files
committed
Initial commit
Completed exercise
1 parent 73d95fe commit bfb23a6

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

chapter07/7-8.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Exercise 7-8. Write a program to print a set of files, starting each new one
3+
* on a new page, with a title and a running page count for each file.
4+
* By Faisal Saadatmand
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
#define LINE 81
11+
#define PAGEBREAK 3840 /* characters per page */
12+
13+
/* functions */
14+
int pagecount(FILE *);
15+
void printcover(char *, int);
16+
17+
int pagecount(FILE *f)
18+
{
19+
int c;
20+
int count = 0;
21+
22+
while ((c = getc(f)) != EOF)
23+
count++;
24+
25+
return (count < PAGEBREAK) ? 1 : count / PAGEBREAK;
26+
}
27+
28+
void printcover(char *s, int n)
29+
{
30+
int i;
31+
32+
printf("************************\n");
33+
printf("File name: %s\n", s);
34+
printf("Page count: %i\n", n);
35+
printf("************************\n");
36+
37+
for (i = 4; i <= (PAGEBREAK / LINE); ++i)
38+
printf("\n");
39+
}
40+
41+
int main(int argc, char *argv[])
42+
{
43+
FILE *fp;
44+
char *prog = argv[0];
45+
int pages;
46+
47+
while (--argc > 0)
48+
if ((fp = fopen(*++argv, "r")) == NULL) {
49+
fprintf(stderr, "%s: can't open %s\n", prog, *argv);
50+
exit(EXIT_FAILURE);
51+
} else {
52+
pages = pagecount(fp);
53+
rewind(fp); /* rewind the input stream */
54+
printcover(*argv, pages);
55+
while (!feof(fp)) /* print file */
56+
putc(getc(fp), stdout);
57+
fclose(fp);
58+
}
59+
exit(EXIT_SUCCESS);
60+
}

0 commit comments

Comments
 (0)