COMP 2710 Software Construction File I/O

Slides:



Advertisements
Similar presentations
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Advertisements

Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
C/C++ Basics (VII) Streams and File I/O Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 3: Input/Output
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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,
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.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Module 4: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 4: I/O In this module we will cover Keyboard/screen input and 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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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++. 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.
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.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 File I/O.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
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.
I/O Streams as an Introduction to Objects and Classes
Chapter 14: Sequential Access Files
What is wrong in the following function definition
ifstreams and ofstreams
COMP 2710 Software Construction Midterm Exam 1 - Review
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
CS-103 COMPUTER PROGRAMMING
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
I/O Streams as an Introduction to Objects and Classes
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
COMP 2710 Software Construction Pointers
Copyright © 2003 Pearson Education, Inc.
Data Streams.
Standard Input/Output Streams
Chapter 6 I/O Streams as an Introduction to Objects and Classes 1.
File I/O.
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,
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.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
File I/O.
CS 144 Advanced C++ Programming February 5 Class Meeting
CHAPTER 4 File Processing.
Reading from and Writing to Files
ifstreams and ofstreams
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

COMP 2710 Software Construction File I/O Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Objectives: I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag settings Stream Hierarchies Preview of inheritance Random Access to Files These slides are adapted from notes by Dr. Walter Savitch (UCSD)

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Introduction Streams Special objects Deliver program input and output File I/O Uses inheritance Not covered until chapter 14 File I/O very useful, so covered here Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-2 2

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Streams A flow of characters Input stream Flow into program Can come from keyboard Can come from file Output stream Flow out of program Can go to screen Can go to file Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-3 3

Streams Usage We’ve used streams already Can define other streams cin Input stream object connected to keyboard cout Output stream object connected to screen Can define other streams To or from files Used similarly as cin, cout 12-4 4

Streams Usage Like cin, cout Consider: Given program defines stream inStream that comes from some file: int theNumber; inStream >> theNumber; Reads value from stream, assigned to theNumber Program defines stream outStream that goes to some file outStream << "theNumber is " << theNumber; Writes value to stream, which goes to file Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-5 5

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Files We’ll use text files Reading from file When program takes input Writing to file When program sends output Start at beginning of file to end Other methods available We’ll discuss this simple text file access here Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-6 6

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. File Connection Must first connect file to stream object For input: File  ifstream object For output: File  ofstream object Classes ifstream and ofstream Defined in library <fstream> Named in std namespace Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-7 7

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. File I/O Libraries To allow both file input and output in your program: #include <fstream> using namespace std; OR #include <fstream> using std::ifstream; using std::ofstream; Why should we include “fstream”? Why should we include “fstream”? Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-8 8

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Declaring Streams Stream must be declared like any other class variable: ifstream inStream; ofstream outStream; Must then "connect" to file: inStream.open("infile.txt"); Called "opening the file" Uses member function open Can specify complete pathname Declare; Open; and Use. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-9 9

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Streams Usage Once declared  use normally! int oneNumber, anotherNumber; inStream >> oneNumber >> anotherNumber; Output stream similar: ofstream outStream; outStream.open("outfile.txt"); outStream << "oneNumber = " << oneNumber << " anotherNumber = " << anotherNumber; Sends items to output file Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-10 10

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. File Names Programs and files Files have two names to our programs External file name Also called "physical file name" Like "infile.txt" Sometimes considered "real file name" Used only once in program (to open) Stream name Also called "logical file name" Program uses this name for all file activity Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-11 11

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Closing Files Files should be closed When program completed getting input or sending output Disconnects stream from file In action: inStream.close(); outStream.close(); Note no arguments Files automatically close when program ends Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-12 12

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. File Flush Output often "buffered" Temporarily stored before written to file Written in "groups" Occasionally might need to force writing: outStream.flush(); Member function flush, for all output streams All buffered output is physically written Closing file automatically calls flush() Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-13 13

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. File Flush Output often "buffered" Temporarily stored before written to file Written in "groups" Occasionally might need to force writing: outStream.flush(); Member function flush, for all output streams All buffered output is physically written Closing file automatically calls flush() Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-14 14

Demo: file_io.cpp Display 12.1 Simple File Input/Output (1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-15 15

File Example: Display 12.1 Simple File Input/Output (1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-16 16

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Exercise Declare and open input file stream fileIn and output file stream fileOut. Attach these to files named input.dat and output.dat. Write #include directives for any required header files. Make certain names from namespaces are accessible. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-17 17

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Answer #include <fstream> . . . ifstream fileIn; ofsream fileOut; fileIn.open(“input.dat”); fileOut.open(“output.dat”); Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-18 18

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Appending to a File Standard open operation begins with empty file Even if file exists  contents lost Open for append: ofstream outStream; outStream.open("important.txt", ios::app); If file doesn’t exist  creates it If file exists  appends to end 2nd argument is class ios defined constant In <iostream> library, std namespace Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-19 19

Alternative Syntax for File Opens Can specify filename at declaration Passed as argument to constructor ifstream inStream; inStream.open("infile.txt"); EQUIVALENT TO: ifstream inStream("infile.txt"); Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-20 20

Character I/O with Files All cin and cout character I/O same for files! Member functions work same: get, getline put, putback, peek, ignore Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-21 21

End of File Check with Read Second method read operation returns bool value! (inStream >> next) Expression returns true if read successful Returns false if attempt to read beyond end of file In action: double next, sum = 0; while (inStream >> next) sum = sum + next; cout << "the sum is " << sum << endl; Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-22 22

End of File Check with Read inStream.eof() double next, sum = 0; while (inStream >> next) sum = sum + next; cout << "the sum is " << sum << endl; Can you implement the above code using eof()? Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-23 23

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Summary Streams connect to files with open operation Stream member functions format output e.g., width, setf, precision Same usage for cout (screen) or files Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-24 24