C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

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.
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
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.
Chapter 7: Files Mr. Dave Clausen La Cañada High School.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
1 Chapter 4 Program Input and the Software Design Process.
An Equal Opportunity University CS 215 Lecture 8 I/O Streams, Error Handling & File Stream Ismail Abumuhfouz.
Chapter 8 Data File Basics.
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 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
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
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?
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++. 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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
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.
Working with Batches of Data
Chapter 14: Sequential Access Files
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
Copyright © 2003 Pearson Education, Inc.
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
RANDOM NUMBER GENERATOR & STREAMS (FILE I/O, STRING)
Today’s Lecture I/O Streams Tools for File I/O
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 9 File Streams Computing Fundamentals with C++ 3rd Edition
File Input and Output.
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Wednesday 09/23/13.
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
CHAPTER 4 File Processing.
Reading from and Writing to Files
Reading Data From and Writing Data To Text Files
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams A very famous bridge over a “stream”

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams A ship

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams in the stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams one at a time

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams A stream of ships

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams an input stream to that famous city

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams No more ships in the stream at this time Let’s process what we just input…

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The C++ input/output library is based on the concept of streams. An input stream is a source of data. An output stream is a destination for data. The most common sources and destinations for data are the files on your hard disk. Reading and Writing Files

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Streams This is a stream of characters. It could be from the keyboard or from a file. Each of these is just a character - even these: which, when input, can be converted to: ints or doubles or whatever type you like. (that was a '\n' at the end of the last line) (No, that was –not- a curse!!!!!!!!!! ¥1,0000,0000 (price of a cup of coffee in Tokyo) Notice that all of this text is very plain - No bold or green of italics – just characters – and whitespace (TABs, NEWLINES and, of course... the other one you can’t see: the space character: (another '\n') (&& another) (more whitespace) and FINALLY: Aren ' t you x-STREAM-ly glad this animation is over? newline characters And there were no sound effects!!!

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The stream you just saw in action is a plain text file. No formatting, no colors, no video or music (or sound effects). The program can read these sorts of plain text streams of characters from the keyboard, as has been done so far. Reading and Writing Files

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved You can also read from files stored on your hard disk: from plain text files (as if the typing you saw had been stored in a file) (or you wanted to write those characters to a disk file). from a file that has binary information (a binary file). Reading and Writing Files The picture of the ship in the stream of ships is stored as binary information.

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved You will learn to read and write both kinds of files. Reading and Writing Files

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved To read or write disk files, you use variables. You use variables of type: ifstream for input from plain text files. ofstream for output to plain text files. fstream for input and output from binary files. Reading and Writing Files

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved To read anything from a file stream, you need to open the stream. (The same for writing.) Opening a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Opening a stream means associating your stream variable with the disk file. Opening a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The first step in opening a file is having the stream variable ready. Here’s the definition of an input stream variable named in_file : ifstream in_file; Looks suspiciously like every other variable definition you’ve done – it is! Only the type name is new to you. Opening a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Suppose you want to read data from a file named input.dat located in the same directory as the program. All stream variables are objects so we will use a method. The open method does the trick: in_file.open("input.dat"); Opening a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved File names can contain directory path information, such as: UNIX in_file.open("~/nicework/input.dat"); Windows in_file.open("c:\\nicework\input.dat"); Opening a Stream ?

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved File names can contain directory path information, such as: UNIX in_file.open("~/nicework/input.dat"); Windows in_file.open("c:\\nicework\input.dat"); Opening a Stream When you specify the file name as a string literal, and the name contains backslash characters (as in a Windows path and filename), you must supply each backslash twice to avoid having escape characters in the string, like '\ n '.

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved If you ask a user for the filename, you would normally use a string variable to store their input. But the open method requires a C string. What to do? Opening a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Luckily most classes provide the methods we need: the string class has the c_str method to convert the C++ string to a C string: Opening a Stream cout << "Please enter the file name:"; string filename; cin >> filename; ifstream in_file; in_file.open(filename.c_str());

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved When the program ends, all streams that you have opened will be automatically closed. You can manually close a stream with the close member function: in_file.close(); Closing a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved You already know how to read and write using files. Yes you do: string name; double value; in_file >> name >> value; Reading from a Stream cout ? in_file ? No difference when it comes to reading using >>. See, I told you so!

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The >> operator returns a “not failed” condition, allowing you to combine an input statement and a test. A “failed” read yields a false and a “not failed” read yields a true. if (in_file >> name >> value) { // Process input } Nice! Reading from a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved You can even read ALL the data from a file because running out of things to read causes that same “failed state” test to be returned: while (in_file >> name >> value) { // Process input } x-STREAM-ly STREAM-lined --- Cool! Reading from a Stream

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The open method also sets a “not failed” condition. It is a good idea to test for failure immediately: in_file.open(filename.c_str()); // Check for failure after opening if (in_file.fail()) { return 0; } Silly user, bad filename! Failing to Open

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Here’s everything: ofstream out_file; out_file.open("output.txt"); if (in_file.fail()) { return 0; } out_file << name << " " << value << endl; out_file << "CONGRATULATIONS!!!" << endl; Writing to a Stream 1. create output stream variable 2. open the file 3. check for failure to open 5. congratulate self! 4. write to file

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Working with File Streams

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved What really happens when reading a string ? string word; in_file >> word; 1.If any reading can be done at all, all whitespace is skipped (whitespace is this set: '\t' '\n' ' ' ). 2.The first character that is not white space is added to the string word. More characters are added until either another white space character occurs, or the end of the file has been reached. Reading string s

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved It is possible to read a single character – including whitespace characters. char ch; in_file.get(ch); The get method also returns the “not failed” condition so: while (in_file.get(ch)) { // Process the character ch } Reading Characters

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The function (it’s not a method): getline() is used to read a whole line up to the next newline character. Reading A Whole Line: getline

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved stream to read from string to read into getline(in_file, line); stops at '\n', takes it from the stream and throws it away – does not store it in string Reading A Whole Line: getline

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Until the next newline character, all whitespace and all other characters are taken and stored into the string – except the newline character – which is just “thrown away” The only type that can be read into is the string type. string line; getline(in_file, line); // stops at '\n' Reading A Whole Line: getline

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The getline function, like the others we’ve seen, returns the “not failed” condition. To process a whole file line by line: string line; while( getline(in_file, line)) { // Process line } Reading A Whole Line: getline

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved You use the operator >> to send string s and numbers to an output stream: ofstream out_file; out_file.open("output.txt"); if (in_file.fail()) { return 0; } out_file << name << " " << value << endl; Writing Text Output

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved But what about a single character? The put method: out_file.put(ch); Writing Text Output

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Sequential Access and Random Access There also two methods of working with files: Sequential Access as we’ve been doing – one input at a time starting at the beginning Random Access Random? rand ?

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Sequential Access and Random Access

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Random Access To change the get positions, you use this method: strm.seekg(position); seekg means move the get position “g” as in “get” – get it? The parameter value is how many bytes from the beginning of the file to move the get position to.

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Random Access seekp does the same for the put position strm.seekp(position);

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Random Access You can also find out where these positions currently are: g_position = strm.tellg(); p_position = strm.tellp();

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved End Chapter Eight Slides by Evan Gallagher