-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.16.4.c
85 lines (65 loc) · 1.45 KB
/
ex.16.4.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
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT 1024
char *get_filename (void);
void remove_newline (char* const str);
FILE *open_file (const char* const fname);
void print_if_line (FILE *file);
void print_interlaced (FILE *file_1, FILE *file_2);
int main (void)
{
char *fname_1, *fname_2;
FILE *file_1, *file_2;
printf ("First file: ");
fname_1 = get_filename ();
file_1 = open_file (fname_1);
printf ("Second file: ");
fname_2 = get_filename ();
file_2 = open_file (fname_2);
print_interlaced (file_1, file_2);
free (fname_1);
free (fname_2);
return 0;
}
char *get_filename (void)
{
char *fname = malloc (MAX_INPUT);
fgets (fname, MAX_INPUT, stdin);
remove_newline (fname);
// discard excess input
if ( strlen(fname) == MAX_INPUT - 1 ) {
while ( getchar() != '\n' );
}
return fname;
}
void remove_newline (char * const str)
{
char *newline;
if ( (newline = strchr(str, '\n')) != NULL ) {
*newline = '\0';
}
}
FILE *open_file (const char * const fname)
{
FILE* file;
if ( (file = fopen (fname, "r")) == NULL ) {
fprintf (stderr, "Unable to open %s for reading.\n", fname);
exit (1);
}
return file;
}
void print_if_line (FILE *file)
{
char buffer[MAX_INPUT];
if ( fgets (buffer, MAX_INPUT, file) != NULL ) {
fputs (buffer, stdout);
}
}
void print_interlaced (FILE *file_1, FILE *file_2)
{
while ( !(feof(file_1) && feof(file_2)) ) {
print_if_line (file_1);
print_if_line (file_2);
}
}