1 Stream Input and Output Read Text, page 23.. 2 Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

C++ Plus Data Structures
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
SW Engineering. 2 Goals Describe the general activities in the software life cycle Describe the goals for "quality" software Explain the following terms:
1 Programming Style Files Lecture 1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva, SUNY-Fredonia.
Chapter 3: Input/Output
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.
Program Input and the Software Design Process ROBERT REAVES.
C++ Vs. Java Who will win?. Look Ma, no classes! C++ was conceived as an object-oriented extension to C C was (is) purely procedural language –Functions.
1 Chapter 4 Program Input and the Software Design Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
1 Chapter 4 Program Input and the Software Design Process.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process CS185/09 - Introduction to Programming Caldwell College.
Chapter 1 Software Engineering Principles. Lecture 2.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
No I/O is built into C++ l instead, a library provides input stream and output stream KeyboardScreen executing program istreamostream 1.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input and the Software Design Process. 2 Chapter 4 Topics  Input Statements to Read Values for a Program using >>, and functions get, ignore,
Chapter 3: Input/Output
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 4 Program Input and the Software Design Process.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
1 Chapter 4 Program Input and the Software Design Process.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
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.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 4 Program Input and the Software Design Process
CS212: Object Oriented Analysis and Design
What Actions Do We Have Part 1
Chapter 1 Software Engineering Principles
Data Streams.
Input and Output Chapter 3.
File I/O.
Lecture 5A File processing Richard Gesick.
Interactive I/O Input from keyboard Must prompt user User friendly
Programming with Data Files
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 3: Input/Output
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
Chapter 3 Input output.
Chapter 4 Program Input and the Software Design Process
Dale/Weems/Headington Program Input and the Software Design Process
Programming Fundamental-1
Presentation transcript:

1 Stream Input and Output Read Text, page 23.

2 Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing program input data output data

3 is header file l for a library that defines 3 objects l an istream object named cin (keyboard) l an ostream object named cout (screen) l an ostream object named cerr (screen)

4 Insertion Operator ( << ) l The insertion operator << takes 2 operands. l The left operand is a stream expression, such as cout. l The right operand is an expression describing what to insert into the output stream. It may be of simple type, or a string, or a manipulator (like endl).

5 Extraction Operator ( >> ) l Variable cin is predefined to denote an input stream from the standard input device ( the keyboard ). l The extraction operator >> called “get from” takes 2 operands. The left operand is a stream expression, such as cin. The right operand is a variable of simple type. l Operator >> attempts to extract the next item from the input stream and store its value in the right operand variable.

6 Extraction Operator >> “skips” (reads but does not store anywhere) leading whitespace characters (blank, tab, line feed, form feed, carriage return) before extracting the input value from the stream (keyboard or file). To avoid skipping, use function get to read the next character in the input stream. cin.get(inputChar);

#include int main( ) { // USES KEYBOARD AND SCREEN I/O int partNumber; float unitPrice; cout << “Enter part number followed by return : “ << endl ; // prompt cin >> partNumber ; cout << “Enter unit price followed by return : “ << endl ; cin >> unitPrice ; cout << “Part # “ << partNumber // echo << “at Unit Cost: $ “ << unitPrice << endl ; return 0; } 7

8 Disk files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “A:\myInfile.dat” disk file “A:\myOut.dat” executing program input dataoutput data #include

9 For File I/O l use #include l choose valid variable identifiers for your files and declare them l open the files and associate them with disk names l use your variable identifiers with >> and << l close the files

10 Statements for using file I/O #include ifstream myInfile; // declarations ofstream myOutfile; myInfile.open(“A:\\myIn.dat”);// open files myOutfile.open(“A:\\myOut.dat”); myInfile.close( );// close files myOutfile.close( );

11 What does opening a file do? l associates the C++ identifier for your file with the physical (disk) name for the file l if the input file does not exist on disk, open is not successful l if the output file does not exist on disk, a new file with that name is created l if the output file already exists, it is erased l places a file reading marker at the very beginning of the file, pointing to the first character in it

#include int main( ) {// USES FILE I/O int partNumber; float unitPrice; ifstreaminFile;// declare file variables ofstreamoutFile; inFile.open(“input.dat”);//open files outFile.open(“output.dat”); inFile >> partNumber ; inFile >> unitPrice ; outFile << “Part # “ << partNumber // echo << “at Unit Cost: $ “ << unitPrice << endl ; return 0; } 12

13 Stream Failure l When a stream enters the fail state, further I/O operations using that stream are ignored. But the computer does not automatically halt the program or give any error message. l Possible reasons for entering fail state include: invalid input data (often the wrong type), opening an input file that doesn’t exist, opening an output file on a diskette that is already full or is write-protected.

#include int main( ) {// CHECKS FOR STREAM FAIL STATE ifstreaminFile; inFile.open(“input.dat”);// try to open file if ( !inFile ) { cout << “File input.dat could not be opened.”; return 1; }... return 0; } 14

15 End