forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_7-8.c
60 lines (50 loc) · 1.03 KB
/
exercise_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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
enum
{
NUM_LINES_TO_NEXT_PAGE = 5
};
void print_heading(char* filename, unsigned page_number)
{
printf("[Page %u] %s\n", page_number, filename);
}
bool print_file_contents(char* filename)
{
FILE* file = fopen(filename, "r");
if (file == NULL)
{
return false;
}
int current_char = EOF;
while ((current_char = fgetc(file)) != EOF)
{
putchar(current_char);
}
return true;
}
void print_blank_lines()
{
for (int i = 0; i < NUM_LINES_TO_NEXT_PAGE; ++i)
{
puts("");
}
}
int main(int argc, char* argv[])
{
if (argc == 1)
{
fprintf(stderr, "%s: no files specified\n", argv[0]);
return EXIT_FAILURE;
}
for (int i = 1; i < argc; ++i)
{
print_heading(argv[i], i);
bool printed_successfully = print_file_contents(argv[i]);
if (printed_successfully && (i != argc - 1))
{
print_blank_lines();
}
}
return EXIT_SUCCESS;
}