Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Advertisements

Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
I/O Streams as an Introduction to Objects and Classes
Copyright © 2002 Pearson Education, Inc. Slide 1.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Chapter 3: Input/Output
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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,
File I/O ifstreams and ofstreams Sections 11.1 &
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
I/O Streams as an Introduction to Objects and Classes
What is wrong in the following function definition
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.
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.
I/O Streams as an Introduction to Objects and Classes
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Input and Output Chapter 3.
File I/O.
Chapter 6 I/O Streams as an Introduction to Objects and Classes 1.
File I/O.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
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.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
Reading from and Writing to Files Part 2
Lecture 9 Files Handling
Reading from and Writing to Files
Presentation transcript:

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Why Use Files? Files allow you to store data permanently! Data output to a file lasts after the program ends An input file can be used over and over No typing of data again and again for testing Create a data file or read an output file at your convenience Files allow you to deal with larger data sets Slide 6- 3

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Declaring an Input-file/Output-file Stream Variable Input-file streams are of type ifstream Declare an input-file stream variable using ifstream in_stream; Ouput-file streams of are type ofstream Declare an input-file stream variable using ofstream out_stream; Slide 6- 4

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Connecting To a File for output ofstream myfile; myfile.open ("example.txt"); Slide 6- 5 Period File name on the disk Double quotes

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Example of Output Stream Slide 6- 6 #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } example.txt Writing this to a file.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Formatted Output Slide 6- 7 #include double a1 = 2.0/3.0; double a2 = 3.0/4.0; double a3 = ; ofstream myfile; myfile.open ("formated.txt"); myfile.setf(ios::fixed); myfile.setf(ios:: showpoint); myfile.precision(3); myfile << a1 << a2 << a3 << endl; myfile << setw(6) << a1 << setw(6) << a2 << setw(6) << a3 << endl; myfile.close(); return 0; } formated.txt

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 6.5 Slide 6- 8 Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Slide 6- 9

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Using fail and exit Immediately following the call to open, check that the operation was successful: in_stream.open("stuff.dat"); if( in_stream.fail( ) ) { cout << "Input file opening failed.\n"; exit(1) ; } Slide Display 6.2

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Class Member Functions The class determines the member functions of the object The class ifstream has an open function Every variable (object) declared of type ifstream has that open function Example: infile1.open("infile.dat"); Slide TypeVariable doublea; ClassObject ifstreaminfile1; Calling object dot Member function

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 6.2 Tools for Streams I/O

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The End of The File Input files used by a program may vary in length Programs may not be able to assume the number of items in the file A way to know the end of the file is reached: The boolean expression (in_stream >> next) Reads a value from in_stream and stores it in next True if a value can be read and stored in next False if there is not a value to be read (the end of the file) Slide 6- 13

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. End of File Example To calculate the average of the numbers in a file double next, sum = 0; int count = 0; while(in_stream >> next) { sum = sum + next; count++; } double average = sum / count; Slide 6- 14

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Stream Names as Arguments Streams can be arguments to a function The function's formal parameter for the stream must be call-by-reference Example: void make_neat(ifstream& messy_file, ofstream& neat_file); Slide 6- 15

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Example Display 6.6 Slide 6- 16

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 6.3 Character I/O

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Character I/O All data is input and output as characters Output of the number 10 is two characters '1' and '0' Input of the number 10 is also done as '1' and '0‘ Get : Reads one character from an input stream Does not skip blanks nor \n Put: Slide char a; cin.get(a); infile.get(a); char x=‘Z’; cin.put(x); outfile.put(x);

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The End of The Line To read and echo a line of input Look for '\n' at the end of the input line: cout<<"Enter a line of input and I will " << "echo it.\n"; char symbol; do { cin.get(symbol); cout << symbol; } while (symbol != '\n'); All characters, including '\n' will be output Slide 6- 19

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Using eof This loop reads each character, and writes it to the screen in_stream.get(next); while (! in_stream.eof( ) ) { cout << next; in_stream.get(next); } ( ! In_stream.eof( ) ) becomes false when the program reads past the last character in the file Slide 6- 20

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. How To Test End of File We have seen two methods while ( in_stream >> next) while ( ! in_stream.eof( ) ) Which should be used? In general, use eof when input is treated as text and using a member function get to read input In general, use the extraction operator method when processing numeric data Slide 6- 21

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Program Example: Editing a Text File The program of Display 6.8… Reads every character of file cad.dat and copies it to file cplusad.dat except that every 'C' is changed to "C++" in cplusad.dat Preserves line breaks in cad.dat get is used for input as the extraction operator would skip line breaks get is used to preserve spaces as well Uses eof to test for end of file Slide 6- 22

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Program Example: Editing a Text File Slide Reads every character of file cad.dat and copies it to file cplusad.dat except that every 'C' is changed to "C++" in cplusad.dat Preserves line breaks in cad.dat get is used for input as the extraction operator would skip line breaks get is used to preserve spaces as well Uses eof to test for end of file

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 6.9 (2/2) Slide Back Next