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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Computer Programming w/ Eng. Applications
CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
Introduction to C Programming
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Program Input and the Software Design Process ROBERT REAVES.
Systems Software Operating Systems.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Linux+ Guide to Linux Certification, Third Edition
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CSE1301 Computer Programming: Lecture 6 Input/Output.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
An operating system (OS) is a collection of system programs that together control the operation of a computer system.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Input from STDIN STDIN, standard input, comes from the keyboard.
CSC201: Computer Programming
Chapter 4 C Program Control Part I
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Revision Lecture
Chapter 18 I/O in C.
A First Book of ANSI C Fourth Edition
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter Four UNIX File Processing.
File Handling.
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 2 - Introduction to C Programming
CST8177 Scripting 2: What?.
Switch Case Structures
Presentation transcript:

Input/output streams

Character Input / Output and Input Validation Input 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 File stdin

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. 4 List folders and files. No input passed. The file descriptor for standard input is 0 (zero) The corresponding variable is FILE* stdin

Standard 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. 5 Rename file. Take data from one file and write to another The 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. 6 The file descriptor for standard error is 2 (two) The corresponding variable is FILE* stderr

There many other streams 7

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! (enter) test.c

Redirection and Files The 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 file Let’s make a simple txt file input.txt Now we can redirect input from this file to our program You can also redirect output to file 10 test.c

The rules governing the use of the two redirection operators ( ) 1.A redirection operator connects an executable program (including standard operating system commands) with a data file. fish.txt > beets.txt addup.exe < count.exe 2.Input 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.txt count.exe > beets.txt > fish.txt 3.Normally, 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 used echo_eof<words.

If redirection doesn't work for you, you can try having the program open a file directly. Use: FILE *fp = fopen(“myfile.txt”, “r”)

Buffers On some systems, the text you input is echoed immediately: HHeelllloo,, tthheerree.. II wwoouulldd[enter] lliikkee aa # On most systems, nothing happens until you press Enter The immediate echoing of input characters is an instance of unbuffered (or direct) input. The delayed echoing, on the other hand, illustrates buffered input, in which the characters you type are collected and stored in an area of temporary storage called a buffer

Why have buffers? 1.It is less time-consuming to transmit several characters as a block than to send them one by one. 2.If you mistype, you can use your keyboard correction features to fix your mistake. Unbuffered input, on the other hand, is desirable for some interactive programs (games). Buffering comes in two varieties: fully buffered I/O: the buffer is flushed (the contents are sent to their destination) when it is full (file input). line-buffered I/O: the buffer is flushed whenever a newline character shows up (keyboard input). A special family of functions, supported by the header file, for unbuffered input.

Terminating Input A computer operating system needs some way to tell where each file begins and ends. One method to detect the end of a file is to place a special character in the file to mark the end. A second approach is for the operating system to store information on the size of the file. C handles this variety of methods by having the getchar() function return a special value EOF when the end of a file is reached. Typically, EOF is defined in the stdio.h file as follows: #define EOF (-1) // the value -1 does not correspond to any character, so it can be used to signal the end of a file.

Terminating keyboard Input: echo_eof.c What if you are reading keyboard input and not a file? Most systems (but not all) have a way to simulate an end-of-file condition from the keyboard. Note these points: 1.You don't have to define EOF because stdio.h takes care of that. 2.You don't have to worry about the actual value of EOF. 3.The variable ch is changed from type char to type int because char variables may be represented by unsigned integers in the range 0 to 255, but EOF may have the numeric value The fact that ch is an integer doesn't faze putchar(). It still prints the character equivalent. 5.To use this program on keyboard input, you need a way to type the EOF character. The system interpret a Ctrl+C anywhere as an end-of-file signal [Ctrl + C]

Working with Buffered Input Buffered input is often a convenience to the user, providing an opportunity to edit input before sending it on to a program, but it can be bothersome to the programmer when character input is used. Program: You pick a number, and the computer tries to guess it. Note that the program makes two guesses every time you enter n.

Working with Buffered Input One solution is to use a while loop to discard the rest of the input line, including the newline character.

Mixing Numeric and Character Input Suppose your program requires both character input using getchar() and numeric input using scanf(). Each of these functions does its job well, but the two don't mix together well. The scanf() function leaves newline in the input queue. getchar() doesn't skip over newline characters

Input Validation You're writing a program that prompts the user to enter a nonnegative integer. Another kind of error is to enter values that are not valid for the particular task the program is doing. For example, that you had a loop that processes nonnegative numbers. One kind of error the user can make is to enter a negative number. You can use a relational expression to test for that: Another potential pitfall is that the user might enter the wrong type of value, such as the character q.

Input Validation You need to dispose of the input that caused the problem in the first place; if scanf() doesn't succeed in reading input, it leaves it in the input queue. Remember on success, scanf() returns the number of items of the argument list successfully filled.

Input Validation The user enters a lower limit and an upper limit defining a range of values. For example, the search may not work with year values less than 1958 or greater than 2004.

The program calculates the sum of the squares of all the integers in a specified range. The program limits the upper and lower bounds of the range to 1000 and –1000, respectively.

Program output

Menu Browsing A menu offers the user a choice of responses. Here's a hypothetical example: Ideally, the user then enters one of these choices, and the program acts on that choice. The program should work smoothly when the user a)follows instructions. b)fails to follow instructions. Tasks Using C's switch user choice can be made to correspond to a particular case label. A while statement is used to provide repeated access to the menu.

Creating menus provides another illustration of how mixing character input with numeric input can cause problems. Suppose, for example, the count() function (choice c) were to look like this: If you then responded by entering 3, scanf() would read the 3 and leave a newline character as the next character in the input queue. The next call to get_choice() would result in get_first() returning this newline character, leading to undesirable behavior.