I'm trying to read a specifically formatted text file into a 2d array however I don't think I am doing it right. This is what I have so far:
int main()
{
int start;
int end;
int ch = 0;
int lines = 0;
int i;
int j;
FILE *fp;
fp = fopen ("file.txt", "r");
rewind(fp);
while(!feof(fp))
{
ch = fgetc(fp);
if (ch == '\n')
lines++;
}
int graph[lines][lines];
memset(graph, 0, sizeof lines);
//I don't think I'm doing the matrix populating correct. PLEASE HELP!
for (i = 0; i < lines; i++)
{
for (j = 0; j < lines; j++)
{
int node;
int edge;
fscanf(fp, "%d,%d", &graph[&node][&edge]);
}
}
}
The while loop and everything in it counts how many lines there are in the text file and the so I set the matrix to the number of lines there are in the file.
This is my text file
2,2 4,6
1,2 3,3 4,8 5,5
2,3 5,7
1,6 2,8 5,9
2,5 3,7 4,9
This is an adjacency list so the first line means node 2 with edge weight 2 and node 4 with edge weight 6 are adjacent to node 1 (first line of text file) so on and so forth.
My problem is, is that I don't know how to put the information from the text file into a matrix. Any advice would help!