C++
1#include <cstdio> // fopen, fclose, fread, fwrite, printf, putchar
2#include <cstdlib> // exit
3using namespace std;
4
5struct Graph {
6 int nodes;
7 int edges;
8 int *nindex;
9 int *nlist;
10 int *eweight;
11};
12
13Graph readGraph(const char *const fname)
14{
15 Graph g;
16 int cnt;
17
18 FILE *f = fopen(fname, "rb");
19 if (f == NULL) {
20 fprintf(stderr, "ERROR: could not open file %s\n\n", fname);
21 exit(-1);
22 }
23 cnt = fread(&g.nodes, sizeof(g.nodes), 1, f);
24 if (cnt != 1) {
25 fprintf(stderr, "ERROR: failed to read nodes\n\n");
26 exit(-1);
27 }
28 cnt = fread(&g.edges, sizeof(g.edges), 1, f);
29 if (cnt != 1) {
30 fprintf(stderr, "ERROR: failed to read edges\n\n");
31 exit(-1);
32 }
33 if ((g.nodes < 1) || (g.edges < 0)) {
34 fprintf(stderr, "ERROR: node or edge count too low\n\n");
35 exit(-1);
36 }
37
38 g.nindex = (int *) malloc((g.nodes + 1) * sizeof(g.nindex[0]));
39 g.nlist = (int *) malloc(g.edges * sizeof(g.nlist[0]));
40 g.eweight = (int *) malloc(g.edges * sizeof(g.eweight[0]));
41 if ((g.nindex == NULL) || (g.nlist == NULL) || (g.eweight == NULL)) {
42 fprintf(stderr, "ERROR: memory allocation failed\n\n");
43 exit(-1);
44 }
45
46 cnt = fread(g.nindex, sizeof(g.nindex[0]), g.nodes + 1, f);
47 if (cnt != g.nodes + 1) {
48 fprintf(stderr, "ERROR: failed to read neighbor index list\n\n");
49 exit(-1);
50 }
51 cnt = fread(g.nlist, sizeof(g.nlist[0]), g.edges, f);
52 if (cnt != g.edges) {
53 fprintf(stderr, "ERROR: failed to read neighbor list\n\n");
54 exit(-1);
55 }
56 cnt = fread(g.eweight, sizeof(g.eweight[0]), g.edges, f);
57 if (cnt == 0) {
58 free(g.eweight);
59 g.eweight = NULL;
60 } else {
61 if (cnt != g.edges) {
62 fprintf(stderr, "ERROR: failed to read edge weights\n\n");
63 exit(-1);
64 }
65 }
66 fclose(f);
67
68 return g;
69}
70
71void writeGraph(const Graph *g, const char *fname)
72{
73 if ((g->nodes < 1) || (g->edges < 0)) {
74 fprintf(stderr, "ERROR: node or edge count too low\n\n");
75 exit(-1);
76 }
77 FILE *f = fopen(fname, "wb");
78 if (f == NULL) {
79 fprintf(stderr, "ERROR: could not create file %s\n\n", fname);
80 exit(-1);
81 }
82 int cnt = fwrite(&g->nodes, sizeof(g->nodes), 1, f);
83 if (cnt != 1) {
84 fprintf(stderr, "ERROR: failed to write nodes\n\n");
85 exit(-1);
86 }
87 cnt = fwrite(&g->edges, sizeof(g->edges), 1, f);
88 if (cnt != 1) {
89 fprintf(stderr, "ERROR: failed to write edges\n\n");
90 exit(-1);
91 }
92
93 cnt = fwrite(g->nindex, sizeof(g->nindex[0]), g->nodes + 1, f);
94 if (cnt != g->nodes + 1) {
95 fprintf(stderr, "ERROR: failed to write neighbor index list\n\n");
96 exit(-1);
97 }
98 cnt = fwrite(g->nlist, sizeof(g->nlist[0]), g->edges, f);
99 if (cnt != g->edges) {
100 fprintf(stderr, "ERROR: failed to write neighbor list\n\n");
101 exit(-1);
102 }
103 cnt = fwrite(g->eweight, sizeof(g->eweight[0]), g->edges, f);
104 if (cnt != g->edges) {
105 fprintf(stderr, "ERROR: failed to write edge weights\n\n");
106 exit(-1);
107 }
108 fclose(f);
109}
110
111int main()
112{
113 int index[4] = { 1, 2, 3, 4 };
114 int list[5] = { 5, 6, 7, 8, 9 };
115 int weight[5] = { 10, 11, 12, 13, 14 };
116
117 Graph g, g2;
118 g.nodes = 4 - 1; g.edges = 5;
119 g.nindex = index; g.nlist = list; g.eweight = weight;
120
121 writeGraph(&g, "graph.data");
122 g2 = readGraph("graph.data");
123
124 for (int i = 0; i <= g2.nodes; i++) printf(" %d", g2.nindex[i]);
125 putchar('\n');
126 for (int i = 0; i < g2.edges; i++) printf(" %d", g2.nlist[i]);
127 putchar('\n');
128 for (int i = 0; i < g2.edges; i++) printf(" %d", g2.eweight[i]);
129 putchar('\n');
130}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/09 05:44