Chapter 14 Sequential Access Files

Similar documents
File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Input and Output File (Files and Stream )

Simple File I/O.

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Fall 2017 CISC/CMPE320 9/27/2017

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Programming II with C++ (CSNB244) Lab 10. Topics: Files and Stream

Unit-V File operations

Developed By : Ms. K. M. Sanghavi

by Pearson Education, Inc. All Rights Reserved. 2

Streams contd. Text: Chapter12, Big C++

File I/O Christian Schumacher, Info1 D-MAVT 2013

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

BITG 1113: Files and Stream LECTURE 10

Chapte t r r 9

Chapter-12 DATA FILE HANDLING

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

After going through this lesson, you would be able to: store data in a file. access data record by record from the file. move pointer within the file

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Advanced I/O Concepts

C++ Programming Lecture 10 File Processing

C++ files and streams. Lec 28-31

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6,

I/O Streams and Standard I/O Devices (cont d.)

C++ does not, as a part of the language, define how data are sent out and read into the program

IS 0020 Program Design and Software Tools

Object Oriented Programming In C++

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Fundamentals of Programming Session 27

CS2141 Software Development using C/C++ Stream I/O

Chapter 3: Input/Output

C++ Input/Output Chapter 4 Topics

by Pearson Education, Inc. All Rights Reserved. 2

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

C++ How to Program 14.6

Chapter 12. Streams and File I/O

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Object Oriented Programming Using C++ UNIT-3 I/O Streams

Streams in C++ Stream concept. Reference information. Stream type declarations

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

C++ Binary File I/O. C++ file input and output are typically achieved by using an object of one of the following classes:

System Design and Programming II

UEE1303(1070) S 12 Object-Oriented Programming in C++

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Lecture 9. Introduction

What we will learn about this week:

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

SHORT REVIEW OF CS TOPICS RANDOM NUMBERS (2 MARKS) which generates a random number in the range of 0 to n-1. For example;

CS242 COMPUTER PROGRAMMING

ios ifstream fstream

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter 1 Introduction to Computers and Programming

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 12: Advanced File Operations

IS0020 Program Design and Software Tools Summer, 2004 August 2, 2004 in Class

Stream States. Formatted I/O

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52

QUESTION BANK. SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING

File I/O CS 16: Solving Problems with Computers I Lecture #9

UNIT- 3 Introduction to C++

Chapter 8 File Processing

CPE Summer 2015 Exam I (150 pts) June 18, 2015

UNIT V FILE HANDLING

Preview 8/28/2018. Review for COSC 120 (File Processing: Reading Data From a File)

Writing a Good Program. 7. Stream I/O

Scientific Computing

C++ Input/Output: Streams

Chapter 3 - Notes Input/Output

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Study Material for Class XII. Data File Handling

Fundamentals of Programming Session 25

Fig: iostream class hierarchy

Chapter 1 INTRODUCTION

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Streams - Object input and output in C++

Understanding main() function Input/Output Streams

Objects and streams and files CS427: Elements of Software Engineering

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Types of files Command line arguments File input and output functions Binary files Random access

Advanced File Operations. Review of Files. Declaration Opening Using Closing. CS SJAllan Chapter 12 2

Lab: Supplying Inputs to Programs

I/O streams

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Generate error the C++ way

Random File Access. 1. Random File Access

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

Files Total: // Files Example 1. #include <iostream> #include <fstream>

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

CS101 Linux Shell Handout

EP241 Computing Programming

Why Is Repetition Needed?

Transcription:

Chapter 14 Sequential Access Files

Objectives Create file objects Open a sequential access file Determine whether a sequential access file was opened successfully Write data to a sequential access file Read data from a sequential access file Test for the end of a sequential access file Close a sequential access file 2

File Types In addition to getting data from the keyboard and sending data to the screen, a program also can get data from and send data to a file on a disk Getting data from a file is referred to as reading the file, and sending data to a file is referred to as writing to the file Files to which data is written are called output files, and files that are read by the computer are called input files 3

File Types (cont d.) Most input and output files are composed of lines of text that are written and read sequentially (in consecutive order, one line at a time) Such files are referred to as sequential access files (also text files, since they store text) You can also create random access and binary access files, which let you access data in random order and according to their byte locations, respectively 4

The CD Collection Program Program manages a CD collection by using a sequential access file to store the names of CDs along with the names of artists Uses two void functions, savecd and displaycds The savecd function gets CD s name and artist s name from the keyboard and saves them in a sequential access file The displaycds function displays contents of sequential access file on screen 5

The CD Collection Program (cont d.) Problem specification and IPO charts for the CD collection program 6

The CD Collection Program (cont d.) IPO charts for the CD collection program (cont d.) 7

The CD Collection Program (cont d.) IPO charts for the CD collection program (cont d.) 8

The CD Collection Program (cont d.) IPO charts for the CD collection program (cont d.) 9

Creating File Objects The iostream file contains the definitions of the istream and ostream classes from which the cin and cout objects, respectively, are created You do not have to create the cin and cout objects in a program because C++ creates the objects in the iostream file for you Objects are also used to perform file input and output operations in C++, but they must be created by the programmer 10

Creating File Objects (cont d.) To create a file object in a program, the program must contain the #include <fstream> directive The fstream file contains the definitions of the ifstream (input file stream) and ofstream (output file stream) objects, which allow you to create input file objects and output file objects Although not required, it is useful to begin input file object names with in and output file object names with out, so as to distinguish a program s input file objects from its output file objects 11

How to create input and output file objects 12

Opening a Sequential Access File You use the open function to open actual files on your computer s disk Syntax is: fileobject.open(filename[, mode]); fileobject: name of existing ifstream or ofstream file object filename: name of file you want to open Function opens file in filename and associates it with fileobject variable 13

Opening a Sequential Access File (cont d.) filename argument may contain an optional path If it does not contain a path, computer assumes the file is located in the same folder as program Optional mode argument indicates how the file is to be opened Use ios::in mode to open a file for input Use ios::out and ios::app to open a file for output 14

Opening a Sequential Access File (cont d.) Use ios::app mode (app stands for append) when you want to add data to the end of an existing file File is created if it does not exist Use the ios::out mode to open a new, empty file File is erased if it already exists Two colons (::) are called scope resolution operators and indicate that the keywords in, out, and app are defined in the ios class 15

Opening a Sequential Access File (cont d.) ios::out is default mode for output file objects Computer uses a file pointer to keep track of the next character to read or write from a file When you open a file for input, the file pointer is positioned at beginning of file When you open a file for output, the file pointer is positioned at beginning of an empty file When you open a file for append, the file pointer is positioned immediately after last character in file 16

How to open a sequential access file 17

Opening a Sequential Access File (cont d.) How to open a sequential access file (cont d.) 18

Position of the file pointer when files are opened for input, output, and append 19

Determining Whether a File Was Opened Successfully open function can fail when attempting to open a file (e.g., path does not exist) You use the is_open function to determine whether a file was opened successfully Returns Boolean value true if the open function was successful; false otherwise Syntax is fileobject.is_open() The! is the Not logical operator, which is used to reverse the truth value of a Boolean expression 20

How to determine the success of the open function 21

Writing Data to a Sequential Access File Syntax for writing data to a file is: fileobject << data A field is a single item of information A record is a collection of one or more related fields To distinguish one record from another, you can write each record on a separate line by including the endl stream manipulator at the end of a statement that writes a record You can separate multiple fields in a record with a character literal constant (e.g., # ) 22

How to write data to a sequential access file 23

Writing Data to a Sequential Access File (cont d.) The employees.txt sequential access file opened in a text editor 24

Reading Information from a Sequential Access File Syntax for reading numeric and char data from a sequential access file is: fileobject >> variablename For string data, you use: getline(fileobject, stringvariablename [, delimitercharacter]); 25

Reading Information from a Sequential Access File (cont d.) How to read data from a sequential access file 26

How to read data from a sequential access file (cont d.) 27

Testing for the End of a Sequential Access File Each time a character is read from a file, the file pointer is moved to the next character When an entire line is read, the file pointer is moved to the next line of the file The eof function determines whether the last character in a file has been read Returns true if file pointer is located at end of file; false otherwise Syntax: fileobject.eof() 28

Testing for the End of a Sequential Access File (cont d.) How to test for the end of a sequential access file 29

Closing a Sequential Access File To prevent loss of data, you use the close function to close a sequential access file as soon as the program is finished using it Syntax: fileobject.close() Function closes the file associated with fileobject so that it can be accessed by other programs or file objects correctly 30

Closing a Sequential Access File (cont d.) How to close a sequential access file 31

Coding the CD Collection Program CD collection program (following slides) uses file input/output concepts presented earlier 32

IPO chart and C++ instructions for the CD collection program 33

IPO chart and C++ instructions for the CD collection program (cont d.) 34

IPO chart and C++ instructions for the CD collection program (cont d.) 35

CD collection program 36

CD collection program (cont d.) 37

CD collection program (cont d.) 38

CD collection program (cont d.) 39

Sample run of the CD collection program 40

Coding the CD Collection Program (cont d.) The cds.txt sequential access file opened in a text editor 41

Summary Sequential access files can be either input files or output files Input files are files whose contents are read by a program Output files are files to which a program writes data To create a file object in a program, the program must contain the #include <fstream> directive You use the ifstream and ofstream classes, which are defined in the fstream file, to create input and output file objects, respectively 42

Summary (cont d.) The file objects are used to represent the actual files stored on your computer s disk After creating a file object, you then use the open function to open the file for input, output, or append You can use the is_open function to determine whether the open function either succeeded or failed to open a sequential access file The is_open function returns true if the open function was successful and false if it failed 43

Summary (cont d.) You can write each record in a file on a separate line by including the endl stream manipulator at the end of each statement that writes a record If the record contains more than one field, you can use a character (such as '#') to separate data in one field from data in another field When reading data from a file, you use the eof function to determine whether the file pointer is at the end of the file 44

Summary (cont d.) If the file pointer is located after the last character in the file, the eof function returns true; otherwise, it returns false When a program is finished with a file, you should use the close function to close it Failing to close an open file can result in loss of data 45