CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.

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.
Lecture 2 Introduction to C Programming
Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Introduction to C Programming
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
COSC 120 Computer Programming
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Guide To UNIX Using Linux Third Edition
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
An Introduction to Operating Systems. Definition  An Operating System, or OS, is low-level software that enables a user and higher-level application.
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.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Chapter Introduction to Computers and Programming 1.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
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.
Introduction to Shell Script Programming
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.
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.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Systems Software Operating Systems. What is software? Software is the term that we use for all the programs and data that we use with a computer system.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Chapter 05 (Part III) Control Statements: Part II.
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:
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.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter Six Introduction to Shell Script Programming.
Input/output streams. Character Input / Output and Input Validation Input and output devices: keyboards, disk drives, mouse, monitors, printers, network.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Validation. Buffering C input is read from a buffer – When the buffer is empty, Characters are moved into it – For terminal, buffer is filled with a line.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
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.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
CSC201: Computer Programming
Chapter 4 C Program Control Part I
Key Ideas from day 1 slides
Revision Lecture
Chapter 18 I/O in C.
Variables, Expressions, and IO
A First Book of ANSI C Fourth Edition
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.
Arrays, For loop While loop Do while loop
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CHAPTER 6 Testing and Debugging.
Programming Logic and Design Eighth Edition
Switch Case Structures
Presentation transcript:

CHARACTER INPUT / OUTPUT AND INPUT VALIDATION

Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. 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.

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!

Buffers When you run the previous program on some systems, the text you input is echoed immediately. That is, a sample run would look like this: HHeelllloo,, tthheerree.. II wwoouulldd[enter] lliikkee aa # On most systems, nothing happens until you press Enter, as in the first example. 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 conio.h 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+Z anywhere as an end-of-file signal

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: The redirection is associated with the operating system, not C. 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. File stdin

Redirection and Files Redirecting Input Redirecting Output Combined Redirection

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.

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. The computer uses a plodding method, but we are concentrating on I/O, not algorithms. 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

Mixing Numeric and Character Input To clear up this problem, the program has to skip over any newlines or spaces between the last number typed for one cycle of input and the character typed at the beginning of the next line. skip newline

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.

Input Validation Consider an example that requires the user to enter a lower limit and an upper limit defining a range of values. For example, the archive search may not work with year values less than 1958 or greater than 2004.

The program uses these two functions to feed integers to an arithmetic function that 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.

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.