Docsity

Standard Input and Output - C Programming - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Standard Input and Output - C Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Standard Input and Output

Overview

  • Data communication with a C program and the

outside world is performed through files

  • Files are a non-volatile way to store data by means

of such media as tape, CD-ROM, floppy disk, ZIP

disk, hard drive, etc.

  • C (just like the UNIX operating system) considers

all process communication media to be files

  • An ordinary file on a disk is considered to be a file
  • So is the keyboard, the screen, parallel ports, and serial ports

stdin, stdout, stderr

  • Three files are automatically opened each time a C

program is run and automatically closed when a C

program ends: stdin, stdout, and stderr

  • stdin
    • Standard input, default source is the keyboard
  • stdout
    • Standard output, default destination is the screen
  • stderr
    • Standard error, default destination is the screen
  • These three variable names are referred to as file

descriptors; each is a handle (or pointer) to a

record (i.e., struct) describing the actual file

C Program Entrances and Exits

stdout (Screen)

stdin (Keyboard)

stderr (Screen)

Constants and variables

Operations and functions

main

X Y Z

A B C D

Instructions and operating procedures

One Byte At a Time

Data is read or written one byte at a time from a file until the end of the file is reached or until the file is closed. The file system uses a pointer to keep track of the next byte to read or to write

Smith Jack 1045.76 Manager 15

Hanson Susan 98.62 Operator 7

Jones Nancy 790.25 Administrator 10

Doe Carl 526.71 Technician 12

In this file, the program has read the data as far as the letter 'J'. The next character to be read is 'a' Docsity.com

Functions Using stdin and stdout-

  • Some standard C functions implicitly use stdin and stdout
    • scanf("%d", &aNumber);
    • aSymbol = getchar();
    • gets(theBuffer); // High security risk
    • printf("Average: %5.2f", theAverage);
    • putchar(aCharacter);
    • puts(aPhrase);

Input and Output Redirection

  • Because a C program considers stdin and stdout to

be ordinary files, a user can redirect the input and

output to another source or destination

  • That source or destination can be an I/O device or

an ordinary file

  • This redirection can be done when a program is

run from the DOS or UNIX command line

  • The ' < ' sign redirects input from a file
  • The ' > ' sign redirects output to a new file
  • The " >> " sign redirects output and appends the

data to a current file or creates the file if it doesn't

yet exist

Example of I/O Redirection

  • Input comes from a file, output goes to the screen
    • C:\myprogram <numbers.dat
  • Input comes from the keyboard, output goes to a file
    • C:\myprogram >results.txt
  • Input comes from a file; output goes to a file
    • C:\myprogram <lab.txt >findings.dat
  • Input comes from the keyboard and the output is

appended to the contents in a file

  • C:\myprogram >>findings.dat

printf Function

  • The printf function writes formatted output to stdout
    • Ex. printf("Average: %5.2f \n", theAverage);
  • The first argument is a character string containing

ordinary text, escape characters, and format specifiers

  • Syntax for format specifier:

%.

  • Each escape character is a combination of a '' and a

character, which represents a nonviewable ASCII

character. An example is \n for newline

  • The order, number, and type of the format specifiers

corresponds to the order, number, and type of the

arguments in the printf call

printf Examples

  • No format specifiers, one escape character, no other

arguments

  • printf("Name Age Address Distance\n");
  • One format specifier, two arguments
  • printf("Results is %d\n", theResult);
  • Four format specifiers, five arguments
  • printf("%c %d %f %s", aCharacter, anInteger, aFloat, aString);
  • Same as above, but with field width and precision

specified

  • printf("%4c %3d %5.3f %8s", aCharacter, anInteger, aFloat, aString);

scanf Examples

  • One format specifier, two arguments
    • scanf("%d", &theResult);
  • Three format specifiers, four arguments
    • scanf("%d%f%s", &anInteger, &aFloat, aString);
  • Same as above, but in a different order
    • scanf("%s %f %d", aString, &aFloat, &anInteger);