I have the next C code:
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "graph.h"
int main() {
int action, src, dst;
IntGraph g;
g = newIntGraph();
while (1) {
printf("1 -> Add a nodes.\n");
printf("2 -> Add an arc.\n");
printf("3 -> Dump the graph.\n");
printf("4 -> BFS.\n");
printf("What do you want to do? [1, 2, 3, 4] ");
scanf("%d", &action);
switch (action) {
case 1:
addIntGraphNode(&g);
break;
case 2:
printf("Insert source and destination: ");
scanf("%d", &src);
scanf("%d", &dst);
addIntGraphArc(&g, src, dst);
break;
case 3:
dumpIntGraph(g, "GRAPH\0");
break;
case 4:
printf("Insert the node to start: ");
scanf("%d", &src);
BFSIntGraph(g, src);
break;
default:
return 0;
}
}
}
But I need it to do some test so I would like to have a ready input that will generate the base graph.
I wrote the input in a file (one number per line). I have a file with ten lines and ten 1, because I want the programm to generate a graph with ten nodes.
When I type:
./graph-test.run < input/graph-input.txt
It starts and endless reading from the file, adding hundreds nodes. I would like it to stop once the file is finished to let me do some other operation.
How can I accomplish this? The code works well if I insert the values manually, so it’s an input related problem.
For every call to
scanfcheck if returns anEOF. In case of anEOFbreak out ofwhile(1)loop.