Iteration over all graph nodes

This example shows how one can read graph from a file and save graph in a file. The input file is assumed to be in GraphViz DOT language.

Sample undirected graph for this example is shown in the figure below:

 1 #pragma package ".."
 2 #include "chagraph.h"
 3 #include <stdlib.h>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7   Agraph_t *g;
 8   Agnode_t *n;
 9 
10   if (argc < 2) {
11     printf("Usage:  %s file\n"
12            "   file -- file in GraphViz dot format\n",
13         argv[0]);
14     exit(EXIT_FAILURE);
15   }
16   FILE *f = fopen(argv[1], "r");
17   if (f == 0) {
18     fprintf(stderr_, "Cannot open file: %s\n", argv[1]);
19     exit(0);
20   }
21   g = agread(f, 0);
22   fclose(f);
23 
24   for (n = agfstnode(g); n; n = agnxtnode(n)) {
25     printf("node: %s\n", agnameof(n));
26   } 
27  
28   agclose(g); 
29   return 0;
30 }

Copyright 2006 Roman Putanowicz

Email: putanowr at twins.pk.edu.pl

Last Modified: Thu, 29 Jun 2006 18:03:12 CEST

Made with PubTal 3.1.3