Click here to load reader

Input/output streams. Character Input / Output and Input Validation Input and output devices: keyboards, disk drives, mouse, monitors, printers, network

  • View

  • Download

Embed Size (px)

Text of Input/output streams. Character Input / Output and Input Validation Input and output devices:...

PowerPoint Presentation

Input/output streamsCharacter Input / Output and Input ValidationInput and output devices: keyboards, disk drives, mouse, monitors, printers, network.I/O functions transport information to and from your program.Standard functions: printf(), scanf(), getchar(), putchar().Other I/O functions take advantage of OS and hardware.Standard streams Standard streams are preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution. 0. standard input (stdin), 1. standard output (stdout),2. standard error (stderr).3


Standard input (stdin)Standard input is data (often text) going into a program. The program requests data transfers by use of the read operation.Not all programs require input. For example, the ls program performs its operation without any stream data input.4List folders and files. No input passed.The file descriptor for standard input is 0 (zero)The corresponding variable is FILE* stdinStandard output (stdout)Standard output is the stream where a program writes its output data. The program requests data transfer with the write operation.

Not all programs generate output. For example the file rename command (mv) is silent on success. the ls program outputs list of files or folders.5Rename file. Take data from one file and write to anotherThe file descriptor for standard output is 1 (one)The corresponding variable is FILE* stdout

Standard error (stderr)Standard error is another output stream typically used by programs to output error messages of diagnostics.It is a stream independent of standard output and can be redirected separately.6The file descriptor for standard error is 2 (two)The corresponding variable is FILE* stderrThere many other streams7

Single-Character I/O

This process is called echoing the input

You might wonder why you must type a whole line before the input is echoed!


test.cRedirection and FilesThe previous program uses the input function getchar().How does a program know where to look for its input?A C program using the standard I/O package looks to the standard input stdin as its source for input. You can tell a program to seek its input from a file instead of from a keyboard.

There are two ways to get a program to work with files:To use special functions that open files, close files, read files, write in files, and so forth.To use a program designed to work with a keyboard and screen, but to redirect input and output along different channels. For example reassign the stdin stream to a file.The redirection is associated with the operating system, not C.Redirecting input from fileLets make a simple txt file input.txt

Now we can redirect input from this file to our program

You can also redirect output to file10


The rules governing the use of the two redirection operators (< and >) A redirection operator connects an executable program (including standard operating system commands) with a data file. fish.txt > beets.txtaddup.exe < count.exeInput cannot be taken from more than one file, nor can output be directed to more than one file by using these operators.addup.exe < fish.txt < beets.txtcount.exe > beets.txt > fish.txtNormally, spaces between the names and operators are optional, except occasionally when some characters with special meaning to the Unix shell or Linux shell or DOS are used. We could, for example, have usedecho_eof