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
|
#include "counts.h"
#include "kv.h"
#include "outname.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
counts_t *countFile(const char *filename, kvarray_t *kvPairs) {
FILE *f = fopen(filename, "r");
if (f == NULL) {
fprintf(stderr, "Could not open file!\n");
return NULL;
}
counts_t *c = createCounts();
char *line = NULL;
size_t sz;
while (getline(&line, &sz, f) >= 0) {
strtok(line, "\n");
addCount(c, lookupValue(kvPairs, line));
}
free(line);
if (fclose(f) != 0) {
fprintf(stderr, "Could not close file!\n");
return NULL;
}
return c;
}
int main(int argc, char **argv) {
// read the key/value pairs from the file named by argv[1] (call the result
// kv)
if (argc == 1) {
fprintf(stderr, "no parameters\n");
return EXIT_FAILURE;
}
kvarray_t *kv = readKVs(argv[1]);
// count from 2 to argc (call the number you count i)
for (int i = 2; i < argc; i++) {
// count the values that appear in the file named by argv[i], using kv as
// the key/value pair
// (call this result c)
counts_t * c = countFile(argv[i], kv);
// compute the output file name from argv[i] (call this outName)
char *outName = computeOutputFileName(argv[i]);
// open the file named by outName (call that f)
FILE *f = fopen(outName, "w");
// print the counts from c into the FILE f
printCounts(c, f);
// close f
// free the memory for outName and c
freeCounts(c);
free(outName);
}
// free the memory for kv
freeKVs(kv);
return EXIT_SUCCESS;
}
|