Stream I/O in C (Lecture 9)

Preparatory Work:

  • New readings: King Chapter 16&17 (structures) or K&R Chapter 6.
  • Review King Chapter 22 or K&R Chapter 7 (file i/o).
  • Prepare King Chapter 17 (dynamic storage) or K&R Chapter 8.
  • The book "Your Unix" has good chapters on vi and emacs, pattern matching (Chapter 15) and regular expressions, CVS and make.

    The following notes should be useful background for both Lab 4 and Assignment #1 and #2.

  • Standard input/output
    Review of fscanf, fgetc, ungetc for i/o streams with mixed character and numeric data.
  • Macros with parameters
    See King Chapter 14 for compiler directives including
    #define and #undef
    #include
    #ifdef and #ifndef

    One formal definition of polynomial term for ease of programming.

    <polynomial> --> {<sign><coeff><exponent>}*
    <sign> --> + | -
    <coeff> --> FLOAT_NUMBER
    <exponent> --> x | x^<degree> | []
    <degree> --> INTEGER

    use fscanf (fid, "%lf", coeff);
    use fscanf (fid, "%d", exponent); or fgetc(fid)

    use fgetc (fid) and ungetc(char, fid) to examine characters.

    Probably best to copy input stream into an array, removing blanks as you go. Build a pointer to the array and then read from the array just like from a file. Ultimately, being able to view files as a data stream in memory is a big asset in C programs.

    As a mathematical note. The polynomial +ax^2 +bx^1 +c can be written:
    c +x(b +x(a)) Polynomials that are rewritten in this form can be evaluated without the need of the pow() function.

  • Press here for other lecture topics

    Look at pointers-1.c and its associated output pointers-1.log,
    and also pointers-2.c and its associated output pointers-2.log.

  • Press here for basis of the next lecture (Structures)