Short Example slide 6 slide 7 Debuggers
int read_graph(FILE * source, /* file to read from or stdin*/ GRAPH * graph, /* the graph that is being updated */ char * progname /* name of the program */ /* PURPOSE: Reads all directed edges from a file (or stdin)into a graph Pre: `source' points to an file open for reading, `graph' points to memory newly allocated for a GRAPH data structure POST: `graph' has been initialized to contain all the edges in the file The number of edges read in has been returned. OR An error message has been sent to stderr and a negative number has been returned. NOTES: Every line of the file should have this format: from to: weight where `from' and `to' are vertex numbers and `weight' is the edge weight. */ { char proc[] = "read_nums"; /* name of function for error messages */ int linenum = 1; int count = 0; /* count of numbers read so far */ char line[MAX_LINE_LEN]; /* buffer to read line from file into */ int from, /* vertex number which the edge leaves */ to, /* vertex number that the edge goes to */ weight; /* the weight of the edge */ if (init_graph(graph) == OKAY) { while((count < MAX_EDGES) && !ferror(source) && !feof(source)) { if (fgets(line, MAX_LINE_LEN, source) != NULL) { linenum++; if (sscanf(line, "%d %d: %d\n", &from, &to, &weight) == 3) { insert_edge(graph, from, to, weight); count++;
} else { (void)fprintf(stderr, "%s[%s()] Error: sscanf() couldn't parse line #%d\n", progname, proc, linenum); (void)fprintf(stderr, "line = \"%s\"\n", line); return(-2); } } else { /* error from fgets() */ if (ferror(source) || !feof(source)) { (void)fprintf(stderr, "%s[%s()] Error: fgets() couldn't read line #%d\n", progname, proc, linenum); return(-3); } } } /* while */ if ((count == MAX_EDGES) || !feof(source)) { (void)fprintf(stderr, "%s[%s()] Warning: using only first %d edges for graph\n", progname, proc, MAX_EDGES); } return(count); } else { (void)fprintf(stderr, "%s[%s()] Error: error from init_graph\n", progname, proc); return(-1); }
short example List of Contents debuggers
Last updated by J. Blustein on 29 May 1996.
This document is copyright by its author, J. Blustein <jamie@csd.uwo.ca>.