Fall 2017 CISC/CMPE320 9/27/2017

Similar documents
Chapter-12 DATA FILE HANDLING

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

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

C++ Programming Lecture 10 File Processing

Chapter 14 Sequential Access Files

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

IS 0020 Program Design and Software Tools

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

Unit-V File operations

Streams contd. Text: Chapter12, Big C++

Advanced I/O Concepts

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

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

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

Lecture 9. Introduction

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

Random File Access. 1. Random File Access

C++ files and streams. Lec 28-31

Fundamentals of Programming Session 27

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

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

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

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

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

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

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

by Pearson Education, Inc. All Rights Reserved. 2

Developed By : Ms. K. M. Sanghavi

Chapter 12: Advanced File Operations

Chapte t r r 9

Fall 2017 CISC124 9/16/2017

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

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

Chapter 8 File Processing

System Design and Programming II

Object Oriented Programming In C++

C++ How to Program 14.6

Study Material for Class XII. Data File Handling

Fall 2017 CISC124 10/1/2017

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

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

Input/Output Streams: Customizing

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

Starting to Program in C++ (Basics & I/O)

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

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

ios ifstream fstream

10/23/02 21:20:33 IO_Examples

CS11 Advanced C++ Lecture 2 Fall

Input and Output File (Files and Stream )

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Reading from files File errors Writing to files

EP241 Computing Programming

Module 11 The C++ I/O System

by Pearson Education, Inc. All Rights Reserved. 2

Fundamentals of Programming Session 25

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

Generate error the C++ way

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

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

Lecture 5 Files and Streams

PIC10B/1 Winter 2014 Exam I Study Guide

COMP322 - Introduction to C++

Short Notes of CS201

More File IO. CIS 15 : Spring 2007

C++ Quick Guide. Advertisements

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Operator overloading

CS201 - Introduction to Programming Glossary By

Consider the following example where a base class has been derived by other two classes:

BITG 1113: Files and Stream LECTURE 10

Object Oriented Programming CS250

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

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

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

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

Fundamental File Processing Operations 2. Fundamental File Processing Operations

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

Module C++ I/O System Basics

Chapter 3 - Notes Input/Output

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

C++ Input/Output Chapter 4 Topics

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:

Fundamentals of Programming Session 28

Documentation. Programming / Documentation Slide 42

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Chapter 11 Customizing I/O

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:

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Introduction. Lecture 5 Files and Streams FILE * FILE *

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

CS3157: Advanced Programming. Outline

COMP322 - Introduction to C++

Week 5: Files and Streams

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

Absolute C++ Walter Savitch

A A B U n i v e r s i t y

Chapter 11 Customizing I/O

Transcription:

Notices: CISC/CMPE320 Today File I/O Text, Random and Binary. Assignment 1 due next Friday at 7pm. The rest of the assignments will also be moved ahead a week. Teamwork: Let me know who the team leader is and of any more membership changes. I know there are problems using bitbucket and hipchat. Let me know if you cannot log in. A Text File I/O Demo. Re-Structure the Demo: Separate Declaration and Implementation. Create a Simple Class. Fall 2017 CISC/CMPE320 - Prof. McLeod 1 Fall 2017 CISC/CMPE320 - Prof. McLeod 2 ISO C++ Stream Hierarchy File I/O istream ostream We ve been including the iostream library for console I/O. istringstream ifstream iostream fstream ofstream ostringstream For file I/O: #include <fstream> So, fstream inherits all the functionality that we are used to from iostream and adds additional functions like open() and close(). Note that iostream extends both istream and ostream. Fall 2017 CISC/CMPE320 - Prof. McLeod 3 Fall 2017 CISC/CMPE320 - Prof. McLeod 4 Text File I/O Reading Two ways to open a file for reading. Assume filename is a string: ifstream filein; filein.open(filename.c_str()); Or: ifstream fileinagain(filename.c_str()); The filename can contain a drive letter and/or folder names a path. If it is a Unix path you are OK, but for a Windows path, remember to use \\ as a folder delimiter. If no path is supplied, the open() function looks in the same folder as your source code (not the *.exe folder ). (Or you can supply a char* literal). Fall 2017 CISC/CMPE320 - Prof. McLeod 5 Fall 2017 CISC/CMPE320 - Prof. McLeod 6 Prof. Alan McLeod 1

Use the fail() function to check to make sure the file has opened OK: To read a char use the get() function: filein.get(achar); if (filein.fail()) { cerr << "Unable to open file: " << filename << endl; return 1; } To read a string, use getline(): string line; getline(filein, line); getline() returns false (or 0) when there is nothing to read in. Fall 2017 CISC/CMPE320 - Prof. McLeod 7 Fall 2017 CISC/CMPE320 - Prof. McLeod 8 Text File I/O Reading, Cont Other ways to check for the end of the file, if you don t know how much to read in advance: while(!filein.eof()) Note that eof() returns true after you have tried to read past the end of the file. Turns out that >> also returns a boolean false if there is nothing to be read: while(filein >> aword) You can also use the re-direction operators, just as you would with screen input: int aval; filein >> aval; Reads an int value into aval. And when you are finished: filein.close(); Fall 2017 CISC/CMPE320 - Prof. McLeod 9 Fall 2017 CISC/CMPE320 - Prof. McLeod 10 Note that the >> operator reads what is expected by examining the type of the variable supplied. It ignores whitespace (tabs, spaces and linefeeds). So, if you wish to read whitespace, you will need to use a get method instead. To create the stream: Text File I/O Writing ofstream outfile("testout.txt"); Use the << operator to write to outfile and then close() the file, when done. To append to a file: ofstream outfile("testout.txt", ios::app); Fall 2017 CISC/CMPE320 - Prof. McLeod 11 ios::app is a constant defined in the iostream library, so no new includes are required. Fall 2017 CISC/CMPE320 - Prof. McLeod 12 Prof. Alan McLeod 2

Text File I/O Writing, Cont. You can also use the fail() function to see if you have had problems opening the file (no write privilige, bad folder, etc.). And, remember the stuff you did in part 3 of exercise 1 (stream manipulators)? This is how you can format things for output. Random File Access Text I/O is all sequential access unidirectional. To open a file for input and output at the same time and to read any position in any order, you need to use random I/O. Same library: fstream. Fall 2017 CISC/CMPE320 - Prof. McLeod 13 Fall 2017 CISC/CMPE320 - Prof. McLeod 14 Random File Access, Cont. To open for input and output, for example: fstream rwstream; rwstream.open( stuff.txt, ios::in ios::out); Last time we used ifstream for input and ofstream for output. fstream extends both of these classes, allowing you to do input and output at the same time. Random File Access, Cont. File open modes: Mode ios::app ios::ate Meaning Append to end of file and you cannot move anywhere else. Starts like append, but then you can move anywhere in the file. ios::in Open for input. ios::out Open for output. ios::trunc Discard contents (default behavior for ios::out). ios::binary Open for binary I/O. Combine modes using the binary or. Fall 2017 CISC/CMPE320 - Prof. McLeod 15 Fall 2017 CISC/CMPE320 - Prof. McLeod 16 Binary Random File Access Imagine two pointers in the file - a read pointer ( get ) and a write pointer ( put ). To read or write you first have to position the pointer at a certain byte position. For example: rwstream.seekp(1000, ios::beg); Positions the write pointer 1000 bytes from the beginning of the file. Binary Random File Access, Cont. rwstream.seekg(1000, ios::end); Positions the read pointer 1000 bytes from the end of the file. You can also use ios::cur to position the pointer in relative terms. After the seek, you read and write the normal way. But how to know where to position the pointers? Fall 2017 CISC/CMPE320 - Prof. McLeod 17 Fall 2017 CISC/CMPE320 - Prof. McLeod 18 Prof. Alan McLeod 3

Binary Random File Access, Cont. This only works when you have a structured file. You need to know what is stored, in which order it is stored and how big each item is. Remember that you can use the sizeof() operator to get the size of anything primitive types or objects. This way you could write an entire object to the file and read it back the same way. Text File I/O Example See TextIODemo.cpp Stuff that also snuck into this demo: Use of vector and string classes. Function prototypes. Passing by constant reference. We will also look at an OOP version of this program. Fall 2017 CISC/CMPE320 - Prof. McLeod 19 Fall 2017 CISC/CMPE320 - Prof. McLeod 20 Function Prototypes Allow you to implement functions in any order, since the function names have already been declared. Easier to see the structure of what you are coding. Parameters must be typed optionally supply the parameter name for style. Next, the declarations will be placed in a separate file from the implementation. File I/O Demo as a Class Restrucure TextIODemo.cpp by separating out a header file and an implementation file. Might as well make a simple class out of the result. See: textfileio.h textfileio.cpp TestFileIOClass.cpp Fall 2017 CISC/CMPE320 - Prof. McLeod 21 Fall 2017 CISC/CMPE320 - Prof. McLeod 22 Header File - textfileio.h This header file only contains the declaration of the TextFileIO class, but it could also have contained: enums structs non-member function prototypes other class declarations constants documentation Putting any implementation in the header file is considered poor structure (and poor style). Declaration in Separate File Now, the implementation can be completely hidden from anyone using your class. You should be able to use the class without knowing anything about the implementation! Fall 2017 CISC/CMPE320 - Prof. McLeod 23 Fall 2017 CISC/CMPE320 - Prof. McLeod 24 Prof. Alan McLeod 4

textfileio.h, Cont. Note the use of const in the member function declaration: vector<string> readfile() const; This contract promises that the member function will not change member variables (attributes). Optional, but good programming practice, particularly for accessors. textfileio.h, Cont. #pragma once ensures that the declarations in this file will only be made once. The *.h file will be included in any file that is going to use this class using: #include "textfileio.h" You can have as many of these #includes as you want in a project without worry! Fall 2017 CISC/CMPE320 - Prof. McLeod 25 Fall 2017 CISC/CMPE320 - Prof. McLeod 26 Class Declaration The public: and private: sections control access to class members from instantiations. As you would expect, encapsulation dictates that your attributes are declared in the private: section! Implementation File textfileio.cpp Has the same name (different extension) as the header file, by convention. Implements member and non member functions. Few comments, or just comments for the developer. Users are not going to see this file. Fall 2017 CISC/CMPE320 - Prof. McLeod 27 Fall 2017 CISC/CMPE320 - Prof. McLeod 28 The constructor: textfileio.cpp, Cont. textfileio.cpp, Cont. Also note the membership operator :: TextFileIO::TextFileIO(const string& fname) : filename(fname) {} Note the shortcut notation in the initialization section. You can still do things the old-fashioned way, especially if you are going to check the arguments for legality. It allows you to associate the member with the class. You can implement members or non-member functions for any header file that you have included. Fall 2017 CISC/CMPE320 - Prof. McLeod 29 Fall 2017 CISC/CMPE320 - Prof. McLeod 30 Prof. Alan McLeod 5

Aside - private Members Member function definitions and their implementations can access private members even if this is in a different file. Non-member functions cannot access private members, only public ones. TestFileIOClass.cpp Some class in your project must have a main function, or your application will not run. (But only one main function per project!) TextFileIO is instantiated on the run-time stack (more about this later) in the usual way. You can only access public: members from here. Fall 2017 CISC/CMPE320 - Prof. McLeod 31 Fall 2017 CISC/CMPE320 - Prof. McLeod 32 Default Constructors Invoked without parameters. If we had one for TextFileIO it would be invoked as: TextFileIO test; No round brackets! What does: TextFileIO test(); look like to you? Fall 2017 CISC/CMPE320 - Prof. McLeod 33 Prof. Alan McLeod 6