Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February 2006 - 1 - - 1 - IO Streams  IOStreams are part of the Standard C++ library.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
. Plab – Tirgul 8 I/O streams Example: string class.
File I/O. Stream Objects cin – the standard input stream - an object of the istream class, cout – the standard output stream – an object of the ostream.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
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.
C++ fundamentals.
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.
C++ Programming Certificate University of Washington Cliff Green
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.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
FILE HANDLING IN C++.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Copyright 2008 Oxford Consulting, Ltd 1 October C to C++ C++ Comments Can use the symbol // To identify single line comments.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
C++ FILE I/O.
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.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Advanced Input and Output Object-Oriented Programming Using C++ Second Edition 10.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
File I/O Version 1.0. Topics I/O Streams File I/O Formatting Text Files Handling Stream Errors File Pointers.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
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.
File Handling in C++.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
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.
Learners Support Publications Working with Files.
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
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.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
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.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
CS212: Object Oriented Analysis and Design
Chapter 8 CS 3370 – C++ I/O Classes.
Tutorial4us.com File Handling in C++ Tutorial4us.com.
C ++ MULTIPLE CHOICE QUESTION
Basic Input and Output Operations
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
Advanced Input and Output
files Dr. Bhargavi Goswami Department of Computer Science
Today’s Lecture I/O Streams Tools for File I/O
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Input/Output Streams, Part 1
Presentation transcript:

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library  Meant to be used in place of C stdio library  So why re-invent the standard io library

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams Considerations for IOStreams vs. printf...  printf uses format strings  printf format strings are interpreted at run time not compile time  With printf binding between variables and values occurs at runtime  With IO streams there are no format strings  Cannot overload printf

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IOStream Class Hierarchy IOS streambuf* readbuf( ) IOS streambuf* readbuf( ) ostream const ostream& operator << (type) const ostream& operator << (streambuf*) ostream const ostream& operator << (type) const ostream& operator << (streambuf*) istrstream ifstream ofstream ostrstream istream const istream& operator >> (type) const istream& operator >> (streambuf*) istream const istream& operator >> (type) const istream& operator >> (streambuf*) streambuf

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Input Streams The extraction Operator >>  Performs the same function as scanf in the C stdio library  Uses white space to delimit its arguments Type at the keyboard Execute code int i, j; cin >> i >> j; i contains 234 j contains 678

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Input Streams The extraction Operator >>  Because istream extraction works like scanf…..  It has many of the same problems Type at the keyboard my name Execute code int i, j; cin >> i >> j; values I and j are left unchanged

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Input Streams istream member functions to the rescue…. get( ) getline( ) Allow us to read an entire line or buffer

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Input Streams get Member Function Prototypes class istream { public:... istream& get(char* ptr, int len, char delim = `\n'); istream& get(char& c); int get(); istream& getline(char* ptr, int len, char delim = `\n'); istream& getline(char& c); int getline(); istream& read(char* ptr, int n); }; class istream { public:... istream& get(char* ptr, int len, char delim = `\n'); istream& get(char& c); int get(); istream& getline(char* ptr, int len, char delim = `\n'); istream& getline(char& c); int getline(); istream& read(char* ptr, int n); };

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Input Streams  Member function get Leaves delimiter in input stream  Member function getline Reads and discards delimiter  Member function read Used to read raw non-delimited bytes

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Output Streams The Insertion Operator <<  Performs the same function as printf in the C stdio library  Its function members are similar to those in the istream class class ostream { public:... ostream& put(char c); ostream& write(const char* buffer, int length); } ;

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Output Streams The Insertion Operator <<  Member Function put Inserts single character into output stream  Member Function write Writes specified number of characters... From designated buffer into output stream

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Error Handling on Input Reading Correct Input Data... IOstreams designed so they can be tested if (cin) if (!cin)  Return true If the last read was successful  Return false If the last read was unsuccessful

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February char buffer[256]; while (cin.getline(myBuff, sizeof(myBuf))) { // process the characters in the buffer. } char buffer[256]; while (cin.get(myBuff, sizeof(myBuf))) { cin.get()// read and discard newline // process the characters in the buffer. } Error Handling on Input

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Error Handling on Input Testing for Errors…  cin.good() Returns true if no errors  cin.eof() Returns true if the input stream is at end of file  cin.fail() Returns true if some input operation has failed  cin.bad() Returns true if something has gone wrong with streambuf

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Error Handling on Input Testing for Errors…  Once an input stream returns an error, it will no longer read any input  To clear an error condition on a stream one must call the clear() error member function Syntax cin.clear();

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February File IO Streams ifstream and ofstream To open file… File stream's constructor is passed the file to open Prototype ofstream output( char* myFileName );

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February File IO Streams ifstream and ofstream void main( void ) { ofstream output("myFile.dat"); if (!output) { cerr << "Oops problem!!! Can’t open myFile.dat" << endl; } return; }

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February File IO Streams ifstream and ofstream Observe the function ignore( ) …. It’s tossing out the newline character left in input stream by the get function Prototype istream& ignore(int n = 1, int delim = EOF); n specifies the number of characters to ignore delim character after which ignore quits.

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February File IO Streams ifstream and ofstream Observe the second argument to the ifstream constructor ios::in Specifies file to be opened for input Default is input on an input stream

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams Seeking in IOStreams... Absolute stream location in a stream called streampos Can obtain current stream position  tellp( ) member function of ostream Identifies the current position in the output streambuf  tellg( ) member function of istream Identifies the current position in the input streambuf To move within a streambuf  seekp member function of ostream  seekg member function of istream

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams Seeking in IOStreams... Prototypes seekp (position); // absolute position seekp (offset, ios::seekdir); // relative position seekg (position); // absolute position seekg (offset, ios::seekdir); // relative position seekdir beg - seek from beginning of current file curr - seek from current position end - seek backwards from end of current file Prototypes seekp (position); // absolute position seekp (offset, ios::seekdir); // relative position seekg (position); // absolute position seekg (offset, ios::seekdir); // relative position seekdir beg - seek from beginning of current file curr - seek from current position end - seek backwards from end of current file

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams Formatting Using Streams… Often when working with display screens necessary to dynamically create messages Useful functions in the C stdio library for doing such formatting functions sprintf and sscanf C++ also provides similar functionality with strstreams. Must include

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams istrstreams… Constructors for istrstream Create an input stream with the string passed to constructor as input istrstream::istrstream(char* buf); istrstream::istrstream(char* buf, int size);

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams ostrstreams… Constructors for ostrstream Create an output stream with the string passed to constructor as input ostrstream::ostrstream(char* buf, int size, int = ios::out); out - characters formatted into starting address of buffer Write pointer positioned at start of buffer app - characters appended to end of buffer Write pointer positioned at null terminator at end of string

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams ostrstreams…  The ostrstream will only allow size characters to be inserted  ostrstream does not automatically insert a null terminator into buffer….Special manipulator ends provided

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams ostrstreams and Storage Allocation  User Specified char myBuffer[128]; ostrstream myString(myBuffer, sizeof(myBuffer)); myString << "Bonjour la Monde" << ends;  User Specified char myBuffer[128]; ostrstream myString(myBuffer, sizeof(myBuffer)); myString << "Bonjour la Monde" << ends;  Automatic ostrstream myString; // myString automatically allocates storage to hold the string. myString << " Bonjour la Monde” << ends;;  Automatic ostrstream myString; // myString automatically allocates storage to hold the string. myString << " Bonjour la Monde” << ends;;

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams Accessing ostrstream Contents... To access the string an ostrstream contains Use member function str( ) ….. ….Returns a pointer to internal buffer holding the string char* cp = A.str();

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams Accessing ostrstream Contents and Freezing the Stream Invoking member function str ( ) freezes the stream Action causes ostrstream return pointer to its internal buffer User invoking str ( ) is now responsible for managing the storage

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February Strstreams Accessing ostrstream Contents and Unfreezing the Stream Frozen stream can be unfrozen... returns pointer to ostrstream Syntax ostrstreamInstance.rdbuff()->freeze(int arg = 1); arg – 0 Unfreeze the stream arg – 1 (or none) Freeze the stream

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams Overloading the Insertion Operator When working with streams a particularly useful operator to overload is insertion operator Can specify how to send data to standard out or to a file Such ability can also be useful for debugging or saving the state of a program

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams Overloading the Insertion or Extraction Operators When overloading insertion or extraction operator Must support constructs of form cout << A << B << C… cin >> J >> L >> M…

Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams Overloading the Insertion or Extraction Operators Syntax ostream& operator<<(ostream& streamObject, const type& instance); streamObject - instance of a steam object passed by reference instance - instance of a user defined variable passed by const reference