forked from ongjinzen/social-network-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphRead.c
More file actions
62 lines (55 loc) · 1.1 KB
/
Copy pathGraphRead.c
File metadata and controls
62 lines (55 loc) · 1.1 KB
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
/** Helper function for Ass2 (COMP2521)
Written By Zain Afzal
**/
#include "Graph.h"
#include "GraphRead.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
Graph readGraph(char* file) {
// ugly count!
FILE * f = fopen(file , "r");
if(!f){
fprintf(stderr, "ERROR READING FILE %s\n", file);
return NULL;
}
int lines = 0;
char ch = 0;
while(!feof(f)){ch = fgetc(f); if(ch == '\n')lines++;}
fclose(f);
//ugly parse!
f = fopen(file , "r");
if(!f){
fprintf(stderr, "ERROR READING FILE %s\n", file);
return NULL;
}
int a=0;
int b=0;
int c=0;
int i = 0;
int maxVert = -1;
int **nums = malloc(sizeof(int*)*lines);
for(i=0;i<lines;i++) nums[i] = malloc(sizeof(int)*3);
i=0;
while(i < lines) {
fscanf(f, "%d,", &a);
fscanf(f, "%d,", &b);
fscanf(f, "%d", &c);
if (a > maxVert) maxVert = a;
if (b > maxVert) maxVert = b;
nums[i][0] = a;
nums[i][1] = b;
nums[i][2] = c;
i++;
}
fclose(f);
Graph g = newGraph(maxVert+1);
i = 0;
while(i < lines) {
insertEdge(g,nums[i][0],nums[i][1],nums[i][2]);
i++;
}
for(i=0;i<lines;i++) free(nums[i]);
free(nums);
return g;
}