INPUT/OUTPUT PART TWO

TOPICS

CREDITS FOR LECTURE AND AUTHORING

Authored by: John Even

Faculty Supervisor: Al Day

Some material is derived from a lesson by: Dr. James Hilliard

HTML Documentaion by: Michelle Roberts

Last Updated: 7/24/96

FORMATTED INPUT

Function
Acquire and transfer data into a storage assigned to specific variable names.

Good Practice
Use the default input unless there is some reason why it cannot be used.

FORMAT FOR scanf

scanf("%cs1%cs2", &var1, &var2);
or
fscanf(fp, "%cs1%cs2", &var1, &var2);

fp
File pointer variable
cs
Conversion Specification for corresonding variables
var
Variable names
  1. For each variable there must be a corresponding conversion specification.
  2. There can be any number of variables, input in the order of occurance.
  3. The fp pointer variable must be initialized before its use.

    FORMAT FOR DIFFERENT VARIABLE TYPES

    # is replaced by any whole number (e.g. 5)

    # is completely optional and may be omitted for default input.

    * Discussed in a Later Lesson.

    HOW TO USE #'S FOR FORMATTED INPUT:

    The number is the Placeholder.

    The placeholder specifies the maximum number of characters to be used in the input conversion (field width).

    When scanning a number into a variable, white spaces preceding the number are ignored. White space includes the space character, the tab character and the newline character (\n).

    EXAMPLES


    b represents a blank space


    * The unused portion of the input line will be saved for later input.

    FILE USAGE

    To use a file, three things must be done.

    1. A pointer file must be declared.
    2. The file must be opened.
    3. fclose must be used on the pointer when finished.

    Declaring a Pointer

    A program that manipulates a specific file must first declare a file pointer variable. File pointer variables are of type FILE*.

    e.g. FILE
    *inp, /*pointer to input file */
    *outp; /* pointer to output file */

    Opening the File

    The file must be prepared for input or output before permitting access to the file. This is what the fopen statement does.

    inp = fopen("circle.dat", "r");
    outp = fopen("cirlce.out", "w");

    inp & outp
    -- previously declared pointer variables
    r
    -- data will be read from the file
    w
    -- data will be written to the file

    fclose

    The file is now ready to be used in I/O.

    e.g. fscanf (inp, "%41f", &radius);

    When a program has no further use for its input and output files, it closes them by calling fclose.

    	e.g.	fclose(inp);
    		fclose(outp);
    

    stdio.h

    In the header file stdio.h, there are also some preset FILE* pointers. These are stdin, stdout, and stderr. Unlike other FILE* pointers, these will send I/O to the keyboard or monitor depending on the type.

      stdin --- keyboard
      stdout --- monitor
      stderr --- monitor
    Using these predeclared pointers, fprintf and fscanf can be used in place of printf and scanf in all cases.

    SCAN/FORMAT EXAMPLES
    Example 1

    fscanf(inp, "%61f", &radius);

    The double variable radius is to be input from the file represented by inp, reading a maximum of 6 columns into the variable radius.

    Example 2

    fscanf(inp, "%4d%10lf", &item, &value);

    The integer variable item and the double variable value are input from columns 1-4 and 5-14, respectively, assuming no white space separates the digits.

    More Examples

    scanf(inp, "%8lf", &radius);

    Computers interpretations:

      - A double number is input from the current data line.
      - The field for this number is up to the first 8 columns of the line, not counting white space.

    CHARACTER INPUT/OUTPUT

    Variables of type char can be scanned and printed in the same fashion as variables of types int and double.

    e.g.:

      scanf("%c", &var);
      printf("The initial is %c", var);
    % c : conversion specification for character type I/O
    var : character variable

    SAMPLE PROGRAM

    #include &ltstdio.h> /* printf, scanf defintions */

    int main (void) {

      char let_1, let_2, let_3;
      /* three letters */
      int year;
      /* current year */
      FILE *outp;
      /* pointer to output file */

      outp = fopen("output.txt", "w");
      printf("Enter a 3-letter nickname and press return> ");
      scanf("%c%c%c", &let_1, &let_2, &let_3);
      printf("Enter the current year and press return> ");
      scanf("%d", &year);
      printf("Welcome, %c%c%c. %d is a great year to study C!\n",
      let_1, let_2, let_3, year);
      fprintf (outp, "%c%c%c", let_1, let_2, let_3);
      fclose(outp);
      return (0);

    }

    PROGRAM OUTPUT

    Enter a 3-letter nickname and press return> Bob
    Enter the current year and press return> 1996
    Welcome, Bob. 1996 is a great year to study C!

    The program also outputs the 3 characters to a data file.