File I/O.

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Object Oriented Programming in C++ IO Streams Kolkata, July 25, 2005.
Advertisements

Getting Data into Your Program
L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.
File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.
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.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
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.
File I/O Supplemental Material. Background In C++, files can be manipulated in the same manner we manipulate streams such as: cout and cin. Therefore,
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
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.
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.
Chapter 8 Data File Basics.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
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 &
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.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
UNIT VI 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 members.
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.
File Handling in C++.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
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.
Program Input and the Software Design Process ROBERT REAVES.
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.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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 © 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.
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.
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.
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.
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
ifstreams and ofstreams
Tutorial4us.com File Handling in C++ Tutorial4us.com.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Functions Manipulating Files
Basic Input and Output Operations
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
Data Streams.
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
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 3: Input/Output
Standard Input/Output Stream
File I/O in C++ I.
File Streams 12/09/13.
ifstreams and ofstreams
COMS 261 Computer Science I
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Presentation transcript:

File I/O

Objectives Learn input and output streams to work with files

Stream Connections Can you use cin or cout to work with files? Input stream (type: iStream): cin Output stream (type: oStream): cout We must use some “file streams” to work with files Data file extension: .dat

Stream Connections File Stream Basics: #include <fstream> // short for file stream Input File Stream type: ifstream Output File Stream type: ofstream

1 2 3 General Usage Flow Declare and connect a file stream object Use it 3 Close it

Reading in Data from a File When would you know to stop reading from a file? Utilize an end-of-file ‘marker’ Utilize the ‘state’ of the file stream objects (both ifstream & ofstream) http://www.cplusplus.com/reference/fstream/ifstream/ End-of-file marker

Reading in Data from a File 1) Utilize an end-of-file ‘marker’ Input:

Reading in Data from a File 1) Utilize an end-of-file ‘marker’ Input: Other string manipulations for data input can be used too, e.g. getline()

Reading in Data from a File 1) Utilize an end-of-file ‘marker’ Output:

Reading in Data from a File 2) Utilize the state of a file stream object Input:

Reading in Data from a File 2) Utilize the state of a file stream object Input: The return value of (fin >> data) is fin (recall the overload of <<) When there is no more data to read from the file (or if some errors occurred), the state of fin will be evaluated as false

Review of operator << overload Reference: LHS RHS return the LHS

Reading in Data from a File 2) Utilize the state of a file stream object Input: The return value of (fin >> data) is fin (recall the overload of <<) When there is no more data to read from the file (or if some errors occurred), the state of fin will be evaluated as false After reading in 10, there will be no more data to read in, so the state of fin becomes false

Reading in Data from a File 2) Utilize the state of a file stream object Output:

Reading in Data from a File

Reading in Data from a File

Using File Stream Objects as Function Parameter When used as a function argument, a file stream object (actually any stream object) must be pass-by-reference.

Using File Stream Objects as Function Parameter Making the function call after fin is successfully connected

Writing Data to a File output.dat will be created if it does not exist If output.dat exists, then new data will overwrite the old data

How would the output file looks like? Writing Data to a File How would the output file looks like?

Writing (append) Data to a File Append the data written out to the end of an existing file http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

Writing (append) Data to a File Append the data written out to the end of an existing file http://www.cplusplus.com/reference/fstream/ofstream/ofstream/ How would the output file looks like?

One More Note on the Operator << Overload

One More Note on the Operator << Overload This insertion operator overload will work for cout It will also works for the output file stream, so you can use it to write to a file