M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS498-006 Lecture 05 Savitch Ch 12.1-12.2 Streams Stream States Input/Output.

Slides:



Advertisements
Similar presentations
Chapter 10 Input/Output Streams
Advertisements

Lecture Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream.
Chapter 2: Basic Elements of C++
M The University Of Michigan Andrew M. Morgan EECS Lecture 01 Savitch Ch. 2 C++ Basics Flow Of Control.
M The University Of Michigan Andrew M. Morgan EECS Lecture 14 Savitch Ch. 9.0 C++ String Data Type C-Strings.
M The University Of Michigan Andrew M. Morgan EECS Lecture 25 Savitch Ch. 15 Polymorphism.
M The University Of Michigan Andrew M. Morgan EECS Lecture 09 Savitch Ch Compiling With Multiple Files Make Utility.
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,
M The University Of Michigan Andrew M. Morgan EECS Lecture 06 Savitch Ch. 5 Arrays Multi-Dimensional Arrays.
M The University Of Michigan Andrew M. Morgan EECS Lecture 24 Savitch Ch. 14 Inheritance.
Savitch N/A Randomness In Programming Simulations
M The University Of Michigan Andrew M. Morgan EECS Lecture 18 Savitch Ch. 17 Linked Data Structures.
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
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.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
1 CS161 Introduction to Computer Science Topic #3.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Chapter 10 Introduction to File I/O Section 10.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
Basic Input and Output Operations
Today in CS161 Week #3 Learn about… Writing our First Program
IO Overview CSCE 121 J. Michael Moore
Manipulators CSCE 121 J. Michael Moore
Input Validation CSCE 121 J. Michael Moore
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Chapter 3: Input/Output
CS150 Introduction to Computer Science 1
What Actions Do We Have Part 1
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
Input Validation CSCE 121 Based on slides created by Carlos Soto.
Introduction to Programming
getline() function with companion ignore()
Programming Fundamental-1
Presentation transcript:

M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 05 Savitch Ch Streams Stream States Input/Output

M M EECS498 EECS498 Andrew M Morgan2 Intro To Streams In C++ a stream is simply "a sequence of bytes" We use a sequence of bytes as input Output is also a sequence of bytes Therefore, there are two main types of streams –Input streams - streams that flow from a device in to memory cin is an input stream - from the keyboard to memory –Output streams - streams that flow from memory out to a device cout is an output stream - from memory to the screen All I/O is a sequence of bytes, even if multi-byte data types are being used –For example, the int value 4,751 is stored in 4 bytes –The data is read in byte-by-byte and can be formed into an int with the value 4,751 by C++ (when you use the >> operator, for example)

M M EECS498 EECS498 Andrew M Morgan3 Buffered I/O Since stream data is read/written byte-by-byte, the data is stored in a "buffer" until the language needs it Data that is input is stored in a buffer until a C++ statement requires information from the buffer to be read by the program int main() { int a; int b; cout << "Enter a: "; cin >> a; cout << "Enter b: "; cin >> b; cout << "A+B=" << a + b << endl; return (0); } Enter a: 5 Enter b: 4 A+B=9 Note: Enter was pressed after each int was input Enter a: 5 4 Enter b: A+B=9 Note: Both integers were entered before enter was pressed. 5 was "consumed" for "a". 4 remained in input buffer and was immediately "consumed" for "b" later in the program.

M M EECS498 EECS498 Andrew M Morgan4 Bad Input Data Type Consider the same program – what if user entered the character 'x' when prompted for "a"? –'x' is not an int, which is what the language expected –Stream enters the "fail state" indicating something has gone wrong –Input requests are ignored when a stream is in the "fail state" int main() { int a; int b; cout << "Enter a: "; cin >> a; cout << "Enter b: "; cin >> b; cout << "A+B=" << a + b << endl; return (0); } Enter a: x Enter b: A+B= Expects and int, received a char. cin stream goes into the "fail state" Since cin stream is still in the "fail state", this input request (and all future input requests) are literally ignored and skipped Therefore, no valid value for either a OR b was provided, and result is garbage.

M M EECS498 EECS498 Andrew M Morgan5 Functionality Associated With cin Several functions are available to help you deal with bad input –cin.good() If this function returns true, then the stream is in a good state, and the next request for input has the possibility of succeeding –cin.fail() If this function returns true, then the stream is in the fail state, and the next request for input will be ignored –cin.eof() A request for input was made after the end-of-file had been reached (indicating there is no more input) –cin.clear() Returns the stream to the good state –cin.ignore(num, endChar) This function "consumes" up to num characters from the stream, up to, and including, the character indicated by endChar (oftentimes the '\n' character)

M M EECS498 EECS498 Andrew M Morgan6 (Bad) Example of Overcoming Fail State What is the output of this program with bad input? int a; bool validInputFound; cout << "Enter a: "; validInputFound = false; while (!validInputFound) { cin >> a; if (cin.fail()) { cout << endl << "Fail state!" << endl; cout << "Try again - Enter a: "; } else { validInputFound = true; } cout << "Entered: " << a << endl; Enter a: x Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! … (infinite loop) Note: While it was determined that cin was in the fail state, it remains in the fail state, and every input request is ignored, resulting in an infinite loop!

M M EECS498 EECS498 Andrew M Morgan7 (Bad) Example of Overcoming Fail State #2 int a; bool validInputFound; cout << "Enter a: "; validInputFound = false; while (!validInputFound) { cin >> a; if (cin.fail()) { cin.clear(); cout << endl << "Fail state!" << endl; cout << "Try again - Enter a: "; } else { validInputFound = true; } cout << "Entered: " << a << endl; Enter a: x Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! Try again - Enter a: Fail state! … (infinite loop) Note: cin stream was cleared of the fail state, but the 'x' that was input remains in the buffer (it is not consumed by the failed input request). As loop iterates it continues trying to consume the 'x' for the value of a, which results in the fail state over and over.

M M EECS498 EECS498 Andrew M Morgan8 (Good) Example of Overcoming Fail State int a; bool validInputFound; cout << "Enter a: "; validInputFound = false; while (!validInputFound) { cin >> a; if (cin.fail()) { cin.clear(); cin.ignore(200, '\n'); cout << endl << "Fail state!" << endl; cout << "Try again - Enter a: "; } else { validInputFound = true; } cout << "Entered: " << a << endl; Enter a: x Fail state! Try again - Enter a: a Fail state! Try again - Enter a: ? Fail state! Try again - Enter a: 4 Entered: 4 cin.clear() takes the stream out of the fail state to allow future requests of input from the stream cin.ignore() consumes all input from the cin input stream, to allow new data to be consumed for future requests of input from the stream

M M EECS498 EECS498 Andrew M Morgan9 Extending Input To Text Files All input discussed has been from keyboard, using the cin stream This is not practical if the amount of data to be input is large –For example: What if you had to input an entire dictionary with the keyboard every time you wanted to spell-check a document? Often, program input will come from a file that is stored with the operating system's file system –Dictionary words could be stored in a file called "dictionary.txt" and a program could read the words from the file, rather than from the keyboard What is contained in a text file? –A sequence of bytes! In other words - a stream You already know how to input from the cin stream –Input from file streams work in much the same way

M M EECS498 EECS498 Andrew M Morgan10 Files As Streams In C++ There are 3 main types of file streams available when you #include –ifstream: Input file stream (sequence of bytes used as input to program) –ofstream: Output file stream (sequence of bytes used as output from program) –fstream: Input and output file stream (sequence of bytes used as input or output) File streams must be associated with a specific file in the file system –This is accomplished by "opening a file" for a file stream –Any input/output operations performed on that file stream interface with the text file – not the keyboard or screen –A file stream that has been opened should then be "closed" when the stream will longer be used in the program I.e. All necessary input has been used, or all data to be output has been output

M M EECS498 EECS498 Andrew M Morgan11 Using An Input File Stream #include using namespace std; int main() { int a; int b; ifstream inFile; inFile.open("inputVals.txt"); inFile >> a; //Similar to "cin >> a;" cout << "Read a: " << a << endl; inFile >> b; cout << "Read b: " << b << endl; cout << "Sum: " << a + b << endl; inFile.close(); return (0); } Contents of inputVals.txt on file system 5 4 Read a: 5 Read b: 4 Sum: 9 Output Of Program Required for using "ifstream" type Indicates that input requests from the stream called "inFile" are to be taken from the sequence of bytes stored in the file named "inputVals.txt". Inputs an integer from the text stored in the file. "File pointer" is updated to indicate where next input from stream will occur from.

M M EECS498 EECS498 Andrew M Morgan12 Problems With Input File Example What is missing from previous example? –Error checking! –Just like the cin stream, file streams may not be in the good state –These result in the file stream being in the fail state The file you tried to open does not exist on the file system Prior input from the stream failed due to an incorrect data type –A stream can also be in the end-of-file state Prior input from the stream was attempted when the stream had already reached the end-of-file character The same functions described for cin (good, fail, eof, clear, ignore) are available for use on input file streams –Error checking should be done to ensure valid input is received!

M M EECS498 EECS498 Andrew M Morgan13 Bad Data In Input File Streams #include using namespace std; int main() { int a; int b; ifstream inFile; inFile.open("inputVals.txt"); inFile >> a; cout << "Read a: " << a << endl; inFile >> b; cout << "Read b: " << b << endl; cout << "Sum: " << a + b << endl; inFile.close(); return (0); } Contents of inputVals.txt on file system x 4 Read a: Read b: Sum: Output Of Program Note: tried to read in 'x' for int variable a, which caused stream to go into the fail state. All future requests for input are ignored, leaving both a and b uninitialized

M M EECS498 EECS498 Andrew M Morgan14 Handling Bad Data In Input File Streams, p.1 #include using namespace std; int main() { int a; int b; ifstream inFile; bool validInputFound; inFile.open("inputVals.txt"); if (inFile.fail()) { cout << "Unable to open input file!" << endl; exit(1); } //Continued on next slide Always check to see if opening the file resulted in a file stream in the good state. If not – the file didn't exist in the file system or the user opening the file did not have access to it.

M M EECS498 EECS498 Andrew M Morgan15 Handling Bad Data In Input File Streams, p.2 //Continued from previous slide validInputFound = false; while (!validInputFound) { inFile >> a; if (inFile.eof()) { cout << "EOF before reading a" << endl; exit(2); } else if (inFile.fail()) { inFile.clear(); inFile.ignore(200, '\n'); } else { validInputFound = true; } cout << "Read a: " << a << endl; //Continued on next slide After input, check to see if file is in the end-of-file state. If so, the input request tried to input data after the end-of-file indicator (which means that there are no more bytes to read in the stream) If end-of-file has not been reached, the input request could have failed resulting in the fail state. Handled the same way as described for cin.

M M EECS498 EECS498 Andrew M Morgan16 Handling Bad Data In Input File Streams, p.3 validInputFound = false; while (!validInputFound) { inFile >> b; if (inFile.eof()) { cout << "EOF before reading b" << endl; exit(2); } else if (inFile.fail()) { inFile.clear(); inFile.ignore(200, '\n'); } else { validInputFound = true; } cout << "Read b: " << b << endl; cout << "Sum: " << a + b << endl; inFile.close(); return (0); } Reading in b is done the same way as a.

M M EECS498 EECS498 Andrew M Morgan17 Outputs Of Example Program Following are outputs of the program, with several different values in input file Contents of inputVals.txt Read a: 51 Read b: 14 Sum: 65 Output Of Program Contents of inputVals.txt x 4 Read a: 4 EOF reached before reading b Output Of Program Contents of inputVals.txt This is for a 51 and now for b 14 Read a: 51 Read b: 14 Sum: 65 Output Of Program

M M EECS498 EECS498 Andrew M Morgan18 End-Of-File For cin Stream End-of-file can be simulated for keyboard input –In UNIX, typing Ctrl-d sends an end-of-file character For this class, we sometimes ask you to check cin.eof() –The reason is because the autograder provides inputs to your program via a file –If the stream reaches end-of-file due to invalid use of input, or for any other reason, your program could go into an infinite loop, delaying your response from the autograder Normally (outside of this class) you won't need to check for cin.eof()

M M EECS498 EECS498 Andrew M Morgan19 Output File Streams Instead of sending output to the screen, output can be sent to a text file #include using namespace std; int main() { int a; int b; ofstream outFile; a = 104; b = -19; outFile.open("output.txt"); if (outFile.fail()) { cout << "Unable to open output file" << endl; exit(1); } outFile << "a: " << a << endl; outFile << "b: " << b << endl; outFile.close(); return (0); } Contents of output.txt file at end of program a: 104 b: -19 Output of program (to screen) (none)