Data Streams.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Getting Data into Your Program
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
 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.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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.
1 File I/O 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.
Chapter 3: Input/Output
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
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.
Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -1 Multiplication * 5 * 4 = 9 Division (integer)
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.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
File I/O ifstreams and ofstreams Sections 11.1 &
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 3: Input/Output
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
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.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Lecture 14 Arguments, Classes and Files. Arguments.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
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.
CS212: Object Oriented Analysis and Design
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Basic Input and Output Operations
COMP 2710 Software Construction File I/O
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
Standard Input/Output Streams
Standard Input/Output Streams
Basic File I/O and Stream Objects
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
File Streams and File I/O
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Chapter 3: Input/Output
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
Chapter 4 INPUT AND OUTPUT OBJECTS
File I/O.
Programming with Data Files
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
Presentation transcript:

Data Streams

Extraction Extraction Operator [ cin>> num1] Extracts formatted data from an input stream Data in the same format as the data type that it is being extracted to. Ex. extracting to an integer will attempt to take whatever is in the stream and conform it to the integer data type. Ignores whitespace

Extraction Questions

Extraction Questions How do we extract whitespace? What happens if the data in the stream doesn’t match the data type? What if there is junk in the stream and we need to get rid of it?

Extraction How do we extract whitespace? Unformatted Extraction Character Extraction The get function – extracts a single character from the stream. String Extraction The getline function – extracts a string from the stream. It needs a delimiter (or just use the default)

Extraction What happens if the data in the stream doesn’t match the data type? Stream failure! No additional information can be extracted from the stream The status of a stream can be checked using two functions. The good function The fail function The stream can be reset back to the good state by using the clear function.

Extraction What if there is junk in the stream and we need to get rid of it? We can clear the stream of all the data in the stream (up to a certain number of characters or up to a delimiter) by using the ignore function.

Insertion Insertion Operator [ cout << num;] Inserts data into an output stream We can immediately send all the data in the stream to the output device using the flush function.

Keyboard Monitor Program.exe cin cout Stream Diagram 1.0

Streams Questions

Streams Questions What other devices can we connect to a program using a stream? How do we write to or read from a file?

Stream Diagram 2.0 Program.exe Keyboard Monitor cin cout File.in File.out Stream Diagram 2.0

Stream Diagram 2.0 Program.exe Keyboard Monitor cin cout File.in File.out Stream Diagram 2.0

File Streams Include the header file for file streams #include <fstream> Declaration of a input file stream ifstream fin; // you can use any variable name Declaration of a output file stream ofstream fout; // you can use any variable name Is that it? Can we use these file streams now?

File Streams Is that it? Can we use these file streams now? No! We need to establish a connection between the file and the program. By using the open function we associate a file stream with a file. fin.open ( “file.in” ); Remember to disassociate a stream with a file after you are done with it. fin.close ( );

Stream Diagram 2.0 Program.exe Keyboard Monitor cin cout File.in File.out fout fin Stream Diagram 2.0

File Streams Questions

File Streams Questions How do we use these streams now?