IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc.

Slides:



Advertisements
Similar presentations
File I/O Finished off Colin Cherry standing in for Dr. Lin.
Advertisements

計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
The Standard String Class Is actually a template: –typedef basic_string string This means you can have strings of things other than chars.
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.
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
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.
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Chapter 12 Streams and File I/O. Learning Objectives I/O Streams – File I/O – Character I/O Tools for Stream I/O – File names as input – Formatting output,
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
File I/O ifstreams and ofstreams Sections 11.1 &
FILE HANDLING IN C++.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
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.
Student Book Input / Output in C++
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
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:
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.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
CS  Inserters and Extractors  Stream State  Files Streams  String Streams  Formatting  Manipulators  Internationalization.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
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.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
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++
Streams & Files in C++:Stream Classes, stream errors, disk file I/O with streams, File Pointers, Error handling in file I/O, File I/O with member functions,
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Chapter 8 CS 3370 – C++ I/O Classes.
Programming with ANSI C ++
17 File Processing.
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
C ++ MULTIPLE CHOICE QUESTION
CISC/CMPE320 - Prof. McLeod
CPSC 231 D.H. C++ File Processing
Standard Input/Output Streams
Standard Input/Output Streams
File I/O Streams, files, strings 1 1.
17 File Processing.
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
Topics Input and Output Streams More Detailed Error Testing
CISC/CMPE320 - Prof. McLeod
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
File I/O in C++ II.
File I/O in C++ I.
Input/Output Streams, Part 2
Presentation transcript:

IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc.

Agenda Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization

Inserters Inserts an object into a stream –that is, it does output Uses operator<< –the left-shift operator –the arrow suggest the direction of the data flow Easy to define for your own classes

Inserter Example A Date class inserter: ostream& operator<<(ostream& os, const Date& d) { char fillc = os.fill('0'); os << setw(2) << d.getMonth() << '-' << setw(2) << d.getDay() << '-' << setw(4) << d.getYear() << setfill(fillc); return os; }

Extractor Example istream& operator>>(istream& is, Date& d) { is >> d.month; char dash; is >> dash; if(dash != '-') { is.putback(dash); is.setstate(ios::failbit); } is >> d.day; is >> dash; if(dash != '-') { is.putback(dash); is.setstate(ios::failbit); } is >> d.year; return is; }

Stream State 4 states: –good –eof –fail (unexpected input type, like alpha for numeric) also set by eof –bad (device failure) Once a stream is no longer good, you can’t use it –all ops are no-ops –Can clear with clear( ) (must after a failed op!) Can test with associated functions: –good( ), eof( ), fail( ), bad( ) Can test for good( ) like this: –if (theStream) [same as if (theStream.good( ))]

Streams and Exceptions Can have exceptions thrown instead of checking state Call the exceptions( ) member function –Can pick which states you want to throw: myStream.exceptions(ios::badbit); The exception type thrown is ios::failure –we’ll see the ios base class later

File Streams Classes ifstream, ofstream, fstream –declared in Constructors open, destructors close All normal stream operations apply Additional member functions: –close( ), open( ) Open modes –ios::in, ios::out, ios::app, ios::ate, ios::trunc, ios::binary –Can combine with the bitwise or ( | )

Stream Buffers The data area(s) held by the stream –One for input, one for output (as declared) –Streams that support both, have both –Can access via the function rdbuf( ) A “Way Station” for data en route Usually don’t worry about it One cool feature: –SType.cpp

Stream Positioning Can move around in a stream –except the console, of course Using functions seekp( ), seekg( ) –seekp( ) seeks in the output buffer (“put”) –seekg( ) seeks in the input buffer (“get”) –Simultaneous I/O streams share the same buffer File streams keep the put/get pointers together In string streams they’re independent Example: IOFile.cpp

Sharing a Buffer Between Streams Multiple streams can process the same source or target Achieved by sharing a stream buffer See hexdec.cpp

String Streams Classes istringstream, ostringstream, stringstream –declared in Writes to or reads from a string –or both but remember the get/put pointers are independent Useful for converting other data types to and from strings Examples: IString.cpp, Ostring.cpp, HTMLStripper2.cpp

Output Formatting Can set stream attributes –width, fill character, alignment, numeric base, floating- point format, decimal precision, etc. Use setf( ) and unsetf( ) Example: Format.cpp

Manipulators A shorthand for setting/unsetting stream attributes –dec, hex, endl, flush Achieved via a special overload convention –manipulators are functions –when inserted, the following function is called: ostream& ostream::operator<<(ostream& (*pf)(ostream&)) { return pf(*this); } The function pf should do its work and return the stream

Creating a Manipulator #include Define a function with the required signature Do your work and return the stream: ostream& nl(ostream& os) { return os << '\n'; } int main() { cout << "newlines" << nl << "between" << nl << "each" << nl << "word" << nl; } cout << nl becomes cout.operator<<(nl), which executes nl(cout), which executes cout << ‘\n’;

Manipulators with Arguments setw(n), setfill(c), setprecision(n), etc. Must include for these Example: Manips.cpp Difficult to implement your own –not portable Use Effectors instead

Effectors Create a class whose constructor formats a string according to its purpose That class also provides an operator<< Example: Effector.cpp

Wide Streams The streams we’ve been using traffic in bytes (char) You can have streams that use wide characters (wchar_t) –displaying foreign characters requires platform support outside of C++ –C++ just stores code points

basic_istream The template that governs the standard stream classes: template > class basic_istream {...}; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ifstream ifstream; typedef basic_ifstream wifstream; typedef basic_istringstream istringstream; typedef basic_istringstream wistringstream;

Locales Cultural customization of I/O formatting A stream has an associated locale Can change it with imbue( ) Example: Locale.cpp (.NET only) Java’s locale support is MUCH better