-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise13.c
150 lines (135 loc) · 2.75 KB
/
exercise13.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// C Primer Plus
// Chapter 13 Exercise 13:
// Do Programming Exercise 12, but use variable-length arrays (VLAs) instead of
// standard arrays.
// Note: use data.txt as input file to test this program.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
FILE *fp;
int ch;
int rows = 0, cols = 0, lastrow_cols;
bool first_line;
if (argc != 3)
{
printf("Usage: %s <data file> <image file>\n", argv[0]);
exit(EXIT_FAILURE);
}
// open data file
if ((fp = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "Could not open file %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
// this passage of code counts the number of columns in each row
// and compares them for consistency, exiting with an error if they're
// not all equal
first_line = true;
while ((ch = getc(fp)) != EOF && isdigit(ch))
{
cols = 1;
while ((ch = getc(fp)) != EOF && ch != '\n')
if (isdigit(ch))
cols++;
if (first_line)
{
first_line = false;
}
else
{
if (cols != lastrow_cols)
{
fprintf(stderr, "Invalid data file: unequal number of columns in rows.\n");
exit(EXIT_FAILURE);
}
}
lastrow_cols = cols;
}
if (cols == 0)
{
fprintf(stderr, "Invalid data file.\n");
exit(EXIT_FAILURE);
}
// this passage counts the number of rows
rewind(fp);
while ((ch = getc(fp)) != EOF)
if (ch == '\n')
rows++;
// with the number of rows and columns counted, the data and img arrays
// can be allocated dynamically
int data[rows][cols];
char img[rows][cols + 1];
// read ints from file
rewind(fp);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (fscanf(fp, "%d", *(data + i) + j) != 1)
{
fprintf(stderr, "Invalid data file.\n");
exit(EXIT_FAILURE);
}
fclose(fp); // done with data file
// convert ints to characters
for (int i = 0; i < rows; i++)
{
int j;
for (j = 0; j < cols; j++)
{
switch (data[i][j])
{
case(0):
ch = ' ';
break;
case(1):
ch = '.';
break;
case(2):
ch = '\'';
break;
case(3):
ch = ':';
break;
case(4):
ch = '~';
break;
case(5):
ch = '*';
break;
case(6):
ch = '=';
break;
case(7):
ch = '}';
break;
case(8):
ch = '&';
break;
case(9):
ch = '#';
break;
default:
fprintf(stderr, "Error: int out of bounds.\n");
exit(EXIT_FAILURE);
}
img[i][j] = ch;
}
img[i][j] = '\0'; // j = cols here
}
// open image file
if ((fp = fopen(argv[2], "w")) == NULL)
{
fprintf(stderr, "Could not open file %s.\n", argv[2]);
exit(EXIT_FAILURE);
}
for (int i = 0; i < rows; i++)
{
// print to console and file
puts(img[i]);
fprintf(fp, "%s\n", img[i]);
}
fclose(fp);
return 0;
}