-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathexercise12.c
119 lines (107 loc) · 2.7 KB
/
exercise12.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
// C Primer Plus
// Chapter 13 Exercise 12:
// Create a text file consisting of 20 rows of 30 integers. The integers should
// be in the range 0–9 and be separated by spaces. The file is a digital
// representation of a picture, with the values 0 through 9 representing
// increasing levels of darkness. Write a program that reads the contents of
// the file into a 20-by-30 array of ints. In a crude approach toward
// converting this digital representation to a picture, have the program use
// the values in this array to initialize a 20-by-31 array of chars, with a 0
// value corresponding to a space character, a 1 value to the period character,
// and so on, with each larger number represented by a character that occupies
// more space. For example, you might use # to represent 9. The last character
// (the 31st) in each row should be a null character, making it an array of 20
// strings. Have the program display the resulting picture (that is, print the
// strings) and also store the result in a text file.
// Note: use data.txt as input file to test this program.
#include <stdio.h>
#include <stdlib.h>
#define ROWS 20
#define COLS 30
int main(int argc, char *argv[])
{
FILE *fp;
int data[ROWS][COLS];
char img[ROWS][COLS + 1];
char ch;
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);
}
// read ints from file
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 or corrupted data file.\n");
exit(EXIT_FAILURE);
}
fclose(fp); // done with fp for now
// 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;
}