-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract data.c
122 lines (90 loc) · 3.37 KB
/
extract data.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long id; // Node identification
char *name;
double lat, lon; // Node position
unsigned short nsucc; // Number of node successors; i. e. length of successors
unsigned long *successors;
}
node;
/*typedef char Queue;
enum whichQueue {NONE, OPEN, CLOSED};
typedef struct {double g, h;
unsigned long parent;
Queue whq;
} AStarStatus;*/
void ExitError(const char *miss, int errcode ){
fprintf ("stderr, \nERROR: %s.\nStopping...\n\n", miss);
exit(errcode);
}
int main (int argc,char *argv[]){
FILE *fin, *ini;
unsigned long nnodes=3472620UL;
char name[257];
node *nodes;
if((nodes=(node *)malloc(nnodes*sizeof(node)) == NULL)) {
ExitError("when allocating memory for the nodes vector", 5);}
printf ("fitxer %s \n", argv[1]);
/*if((ini=fopen(argv[1],"r"))==NULL){
ExitError("the data file does not exist or cannot be opened", 11);
}
else{printf("file open\n");}
fread(nodes,sizeof(node),nnodes,ini);
printf ("tots els sucesors %d \n",nnodes);
fread(allsuccessors, sizeof(unsigned long),20,ini);
fclose(ini);
for(int i=0; i<nnodes; i++)if(nodes[i].nsucc){
nodes[i].successors = allsuccessors; allsuccessors += nodes[i].nsucc;
printf("nodes %d",nodes[i].successors);}
*/
/* llegir datos binaris
if((ini=fopen(argv[1],"r"))==NULL){
ExitError("the data file does not exist or cannot be opened", 11);
}
if (fread(&nnodes, sizeof(unsigned long),1,ini)+fread(&ntotsucc, sizeof(unsigned long),1,ini) !=2){
ExitError("when allocating memory for the nodes vector", 13);
}
if ((allsuccessors=(unsigned long *) malloc(ntotnsucc*sizeof(unsigned long))) ==NULL){
ExitError("when allocating memory for the edges vector", 15);
}
if (fread(nodes,sizeof(node),nnodes,ini)!=nnodes){
ExExitError("when reading nodes from the binary data file", 17);
}
if(fread(allsuccessors, sizeof(unsigned long),ntotnsucc,ini)!=ntotnsucc){
ExitError("when reading sucessors from the binary data file", 18);
}
fclose(ini);
for(i=0; i<nnodes; i++)if(nodes[i].nsucc){
nodes[i].successors = allsuccessors; allsuccessors += nodes[i].nsucc;
}
*/
/*escriure binari*/
unsigned long ntotnsucc=0UL;
printf ("sucesors %f \n", ntotnsucc);
printf ("nombre de nodes %d \n", nnodes);
for(int i=0; i<nnodes; i++){
printf ("iteracio %d \n", i);
ntotnsucc += nodes[i].nsucc;
printf ("sucesors %f \n", ntotnsucc);
}
strcpy(name,argv[2]);
strcpy(strrchr(name,'.'), "bin");
if((fin=fopen(name,"wb"))==NULL) {
ExitError("the output binary data file cannot be opened", 31);
printf ("ntotnsucc\n");}
if(fwrite(&nnodes, sizeof(unsigned long),1,fin)+fwrite(&ntotnsucc, sizeof(unsigned long),1,fin)!=2){
ExitError("when initializing the output binary data file", 32);
printf ("ntotnsucc\n");}
if(fwrite(nodes, sizeof(node),nnodes,fin) != nnodes){
ExitError("when writing nodes to the output binary data file", 32);
printf ("ntotnsucc\n");}
for (int i=0; i<nnodes; i++){
if(nodes[i].nsucc){
if(fwrite(nodes[i].successors,sizeof(unsigned long), nodes[i].nsucc,fin) != nodes[i].nsucc){
ExitError("when writing edges to the output binary data file", 32);
printf ("ntotnsucc\n");}
} }
fclose(fin);
}