Currently i'm trying to read numbers for a graph. I'm using an array of vectors because i know the amount of vertices i'm using. So the text file looks like this:
1 2 3 4
2 1
3 1
4 1
So my print functions works perfectly and i'm trying to get it to print like this:
Adjacency list of vertex 0
head
Adjacency list of vertex 1
head -> 2-> 3-> 4
Adjacency list of vertex 2
head -> 1
Adjacency list of vertex 3
head -> 1
Adjacency list of vertex 4
head -> 1
But it does not stop here. If i add a printf("%d", number);
at the end, it stops before any issues with seg fault ( i understand that it is C but i used it by mistake and noticed it stopped right before doing anything wrong.
Here is my main:
int main()
{
int V = 5;
vector<int> adj[V];
FILE *file = fopen("bfs1.txt", "r");
if(file == 0){
cout << "ERROR: file does not exist" << endl;
return -1;
}
else {
int x;
fscanf(file, "%d", &x);
int indexChange = 1;
while(!feof(file)) {
int c = getc(file);
int number = c - '0';
//to ignore spaces
if (number == -16 ){
continue;
}
//NewLine so increase vertex index
if (number == -38){
indexChange = indexChange + 1;
continue;
}
//End of file
if (number == -49){
printGraph(adj,V);
break;
}
addEdge(adj, indexChange, number);
printGraph(adj,V);
//printf("%d\n", number);
}
}
return 0;
}
With this changes it worked for me but i don't know why its working.
if (number == -38){
indexChange = indexChange + 1;
break;
}