CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]

Slides:



Advertisements
Similar presentations
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Advertisements

1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
File streams Chapter , ,
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/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.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
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.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
Chapter 3: Input/Output
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
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.
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.
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.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
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?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
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.
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
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
ifstreams and ofstreams
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.
Basic Input and Output Operations
Copyright © 2003 Pearson Education, Inc.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
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
Managing console i/o operations
Chapter 3 Input output.
Formatted Input, Output & File Input, Output
Reading from and Writing to Files
ifstreams and ofstreams
Dale/Weems/Headington Program Input and the Software Design Process
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Reading from and Writing to Files
Programming Fundamental-1
Presentation transcript:

CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]

Standard Streams (review) Standard streams defined in iostream.h : cin: standard input stream (usually keyboard) cout: standard output stream (usually screen) cerr: standard error stream (also usually directed to the screen) Operations “get from”: cin >> k >> j; “put to”: cout << “the answer is ” << z << endl;

Formatted Output Various “manipulators” in <iomanip.h> can be included in the output stream to control the appearance of the next thing written. cout.setf(ios::fixed,ios::floatfield); cout << setprecision(3) << 12.34; produces 12.340 Available operations include left/ right justify, set width, set number of digits of precision, …. See textbook for list.

<iomanip> In the std namespace #include <iomanip> // To drop std:: notation using namespace std; Supports more manipulators than iomanip.h cout << fixed << setprecision(3) << 3.14159; produces 3.142

Stream I/O for New Types Stream operations >>, << are defined in C++ for basic types New definitions of >>, << can be given for user-defined types (classes) // write Complex number as <real,imag> ostream& operator<<(ostream &s,Complex z){ return s << ‘<‘ << z.re << ‘,’ << z.im << ‘>’; } This allows cout<<x<<y; How?

How cout<<x<<y works >> and << are binary left-associative operations. The meaning of cout << x << y << z; is ((cout << x) << y) << z; So after writing desired output, operator<< must return a reference to the stream so it can be used for further output.

Stream States All streams have a “state”. All streams are objects (instances of stream classes) Several member functions are available to check or set state. cin.eof();//true if cin eof reached cin.clear(); // set state to good The stream itself can be used in an expression to check its state if (!cin) cerr << “error on cin” << endl;

Input Errors Stream input “fails” if the next thing in the input has the wrong format or if there is no more data (end of file). If an input operation fails, the variable involved is not changed. if (cin >> k) cout << "new value for k read ok"; else cout << "input failed; " << "k not changed";

Input Errors (cont) Once a stream input operation has failed, any further operations will also fail until the stream state is reset. // suppose next input is “xyz” cin >> k; // fails; k unchanged cin >> j; // cin state not good, // so nothing happens cin.clear(); // cin can be used // for input again

Example: Copy Integers This program copies integers from cin to cout until an input operation fails. Each integer is written on a separate output line. #include <iostream.h> int main() { int j; while (cin >> j) cout << j << ‘\n’; return 0; }

Unformatted Stream I/O >> and << provide formatted I/O. Member functions provide unformatted (character-level) I/O. Examples: char ch; char s[100]; cin.get(ch); //read 1 character into ch cin.getline(s); //read next line into s cout.put(ch); //write 1 character ch Variations available to limit how many characters are read, specify end-of-line characters, etc.

File Concepts (review) Input (read) vs output (write) File name (on disk) vs file variable (in program) open close end of file

Stream Classes cin and cout are defined in <iostream.h>. Library <fstream.h> contains similar classes for file I/O Input stream classes: istream: console input (cin) ifstream: file input Output stream classes ostream: console output (cout, cerr) ofstream: file output

File I/O To open a file, give the disk file name as an argument when the file variable is created. #include <fstream.h> int main() { int k; ifstream here(“source.txt”); ofstream there(“dest.txt”); //copy one integer from here to there here >> k; there << k; } Files are automatically closed when the program terminates (can also use the close member function)

File I/O Example Example: Copy contents of user-specified input file to user-specified output file. #include <fstream.h> int main() { char filename[300],ch; cout << "input file? "; cin >> filename; ifstream source(filename); cout << "output file? "; ofstream dest(filename); while (source.get(ch))dest.put(ch); return 0; }

File I/O Example This version ignores various possible errors. What are they? Opening the input or output files fails Error while copying source to dest

Stream Class Relationships Every ifstream (file) is also a istream. An ifstream is an “enhanced” istream that has extra capabilities to work with disk files An ifstream object can be used wherever an istream object is needed (function parameter, for example) But the reverse is not true. An istream is not also an ifstream. So if an ifstream is explicitly called for, cin can’t be used A similar relationship holds between ofstreams and ostreams.