-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_count.c
92 lines (76 loc) · 2.24 KB
/
file_count.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
#include <stdio.h>
#include <stdlib.h>
#include "file_count.h"
//typedef struct {
// int users;
// int cities;
// int states;
// int messages;
// int timestamps;
// int datestamps;
//} file_count_t;
void print_file_count(file_count_t *fc)
{
if (fc == NULL) {
fprintf(stderr, "The file_count is NULL\n");
exit(0);
}
printf("%-11s: %d\n","Users", fc->users);
printf("%-11s: %d\n","Cities", fc->cities);
printf("%-11s: %d\n","States", fc->states);
printf("%-11s: %d\n","Messages", fc->messages);
printf("%-11s: %d\n","Timestamps", fc->timestamps);
printf("%-11s: %d\n","Datestamps", fc->datestamps);
printf("%-11s: %d\n","Total", fc->users + fc->cities + fc->states + fc->messages + fc->timestamps + fc->datestamps);
}
file_count_t *read_file_count()
{
// open file
FILE *fp;
fp = fopen("file_count.dat", "rb");
if (!fp) {
printf("Unable to open file_count.dat. Make sure you run make_tables first!\n");
exit(0);
}
// allocate memory for the record
file_count_t *fc = (file_count_t *)malloc(sizeof(file_count_t));
// memory error
if (fc == NULL) {
fprintf(stderr, "Cannot allocate memory for file_count.\n");
exit(0);
}
// read file_count
fread(&(fc->users), sizeof(int), 1, fp);
fread(&(fc->cities), sizeof(int), 1, fp);
fread(&(fc->states), sizeof(int), 1, fp);
fread(&(fc->messages), sizeof(int), 1, fp);
fread(&(fc->timestamps), sizeof(int), 1, fp);
fread(&(fc->datestamps), sizeof(int), 1, fp);
fclose(fp);
return fc;
}
void write_file_count(file_count_t *fc)
{
// open file
FILE *fp;
fp = fopen("file_count.dat", "wb");
if (!fp) {
printf("Unable to open file_count.dat for writing.\n");
exit(0);
}
// read file_count
fwrite(&(fc->users), sizeof(int), 1, fp);
fwrite(&(fc->cities), sizeof(int), 1, fp);
fwrite(&(fc->states), sizeof(int), 1, fp);
fwrite(&(fc->messages), sizeof(int), 1, fp);
fwrite(&(fc->timestamps), sizeof(int), 1, fp);
fwrite(&(fc->datestamps), sizeof(int), 1, fp);
fclose(fp);
}
void free_file_count(file_count_t *fc)
{
if (fc == NULL) {
return;
}
free(fc);
}