Scholarly Resources for CompSci Undergrads

C Programming Language

Notes about C Debugging (slides 6 and 7)

Back to Short Example down to slide 6 down to slide 7 Forward to Debuggers


Slide 6: A Longer Example


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++;

Slide 7: A Longer Example (error handling)


            } 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);
   }

Where to next?

These slides

back up to Slide 6 back up to Slide 7

Other slides

Back to short example Up to List of Contents Forward to debuggers


http://www.csd.uwo.ca/~jamie/C/Debug/longer_example.html

Last updated by J. Blustein on 29 May 1996.

This document is copyright by its author, J. Blustein <jamie@csd.uwo.ca>.

HTML 2.0 Checked!