Developed By : Ms. K. M. Sanghavi

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

Developed By : Ms. K. M. Sanghavi

C++ files and streams. Lec 28-31

File handling Basics. Lecture 7

Unit-V File operations

Chapter-12 DATA FILE HANDLING

Chapte t r r 9

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

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

Chapter 12: Advanced File Operations

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

Study Material for Class XII. Data File Handling

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

Chapter 14 Sequential Access Files

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

All About: File I/O in C++ By Ilia Yordanov, ; C++ Resources

Random File Access. 1. Random File Access

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

Object Oriented Programming In C++

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

More File IO. CIS 15 : Spring 2007

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

C++ Programming Lecture 10 File Processing

UNIT V FILE HANDLING

Writing a Good Program. 7. Stream I/O

System Design and Programming II

Chapter 12 File Operations. Starting Out with C++, 3 rd Edition

Module 11 The C++ I/O System

IS 0020 Program Design and Software Tools

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

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

by Pearson Education, Inc. All Rights Reserved. 2

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

Fall 2017 CISC/CMPE320 9/27/2017

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

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Ch - 7. Data File Handling

Streams contd. Text: Chapter12, Big C++

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

ios ifstream fstream

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

Lecture 9. Introduction

Introduction. Lecture 5 Files and Streams FILE * FILE *

Lecture 5 Files and Streams

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

Chapter 8 File Processing

Applications with Files, Templates

Fundamentals of Programming Session 27

Input and Output File (Files and Stream )

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

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

Object Oriented Programming CS250

DATA FILE HANDLING FILES. characters (ASCII Code) sequence of bytes, i.e. 0 s & 1 s

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

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

Page 1

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

Stream States. Formatted I/O

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

Advanced I/O Concepts

Computer programs are associated to work with files as it helps in storing data & information permanently. File - itself a bunch of bytes stored on

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions

And Even More and More C++ Fundamentals of Computer Science

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Files and Streams. 1 P a g e

UNIT V FILE HANDLING

C++ How to Program 14.6

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

BITG 1113: Files and Stream LECTURE 10

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

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

Object Oriented Programming

Chapter 3 - Notes Input/Output

Fig: iostream class hierarchy

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

Generate error the C++ way

Downloaded from

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

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

by Pearson Education, Inc. All Rights Reserved. 2

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

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

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

Lecture 3 The character, string data Types Files

Chapter 12. Streams and File I/O

CS11 Advanced C++ Lecture 2 Fall

UNIT-5. When a C++ program begins execution, four built-in streams are automatically opened. They are: Stream Meaning Default Device

by Pearson Education, Inc. All Rights Reserved. 2

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

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

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

Streams. Rupesh Nasre.

Fundamental File Processing Operations 2. Fundamental File Processing Operations

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

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

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

Streams - Object input and output in C++

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CS201 Latest Solved MCQs

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

Transcription:

Developed By : Ms. K. M. Sanghavi

Designing Our Own Manipulators We can design our own manipulators for certain special purpose.the general form for creating a manipulator without any arguments is: ostream & manipulator(ostream & output) { (code) return output } Here the manipulator is the name of the manipulator under creation.

The following function defines a manipulator called unit that dispalys inches : ostream & unit(ostream &output) { output<< inches ; return output; } The statement cout<<36<<unit; will produce the following output 36 inches

We can also create manipulators that could represent a sequence of operations. Example: ostream & show(ostream & output) { output.setf(ios::showpoint); output.setf(ios::showpos); output<<setw(10); return output; }

Program illustrates the creation and use of the user-defined manipulators. The program creates two manipulators called currency and form which are used in the main program.

#include<iostream.h> #include<iomanip.h> ostream & currency(ostream & output) { output<< Rs ; return output; }

ostream& form(ostream & output) { output.setf(ios::showpos); output.setf(ios::showpoint); output.fill( * ); output.precision(2); output<<setiosflags(ios::fixed)<<setw(10); return output; } int main() { cout<<currency <<form<<7864.5; return 0; }

The output of Program would be: Rs**+7864.50

Stream Errors bool ios::good() : returns true if no error bool ios::bad() : returns true if invalid read/write bool ios::eof() : returns true if end of file reach bool ios::fail() : returns true if input operation fails void ios::clear() : clears all the flags

int main() { int num; bool valid = false; while (!valid) { valid = true; //Assume the cin will be an integer. } cout << "Enter an integer value: " << endl; cin >> num; if(cin.fail()) //cin.fail() checks to see if the value in the cin //stream is the correct type, if not it returns true, //false otherwise. { cin.clear(); //This corrects the stream. cin.ignore(); //This skips the left over stream data. cout << "Please enter an Integer only." << endl; valid = false; //The cin was not an integer so try again. }

cout << "You entered: " << num << endl; } system("pause"); return 0;

File I/O Streams Stream ifstream ofstream fstream Description Reads from files Writes on files Read & Write From/To files To perform File I/o We include <fstream.h> in the program

ifstream Input file stream Class open() is a member function of the class ifstream Inherited functions of ifstream class, from the class istream are get() getline() read() seekg() tellg()

ofstream Output file stream Class open() is a member function of the class ofstream Inherited functions of ofstream class, from the class ostream are put() write() seekp() tellp()

File Handling Classes

Opening a File Use method open() File Handling Classes Or immediately in the constructor (the natural and preferred way). copyrights Elhanan Borenstein

Opening a File Before data can be written to or read from a file, the file must be opened. ifstream inputfile; inputfile.open( customer.dat ); Another Syntax void open(const char* filename, int mode); filename file to open (full path or local) mode how to open (one or more of the following using ) 17

Modes can be File Handling Classes ios::app append ios::ate open with marker at the end of the file ios::in / ios::out (the defaults of ifstream and ofstream) ios:nocreate / ios::noreplace open only if the file exists / doesn t exist ios::trunc open an empty file ios::binary open a binary file (default is textual) Don t forget to close the file using the method close()

Opening a File at Declaration fstream f; f.open( names.dat, ios::in ios::out ios:app); 19

Testing for Open Errors datafile.open( cust.dat, ios::in); if (!datafile) { cout << Error opening file.\n ; } 20

Querying a File is_open() Checking whether the file was open correctly. (for compatibility with C, the operator! was overloaded). rd_state() returns a variable with one or more (check with AND) of the following options: ios::goodbit OK ios::eofbit marker on EOF File Handling Classes ios::failbit illegal action, but alright to continue ios:badbit corrupted file, cannot be used. We can also access the bit we wish to check with eof(), good(), fail(), bad(). clear() is used to clear the status bits (after they were checked). copyrights Elhanan Borenstein

Another way to Test for Open Errors f.open( cust.dat, ios::in); if (f.fail()) { cout << Error opening file.\n ; } 22

Detecting the End of a File The eof() member function reports when the end of a file has been encountered. if (f.eof()) f.close(); 23

Moving within the File seekg() / seekp() moving the reading (get) / writing (put) marker two parameters: offset and anchor File Handling Pointers tellg() / tellp() getting the position of the reading (get) / writing (put) marker copyrights Elhanan Borenstein

Reading /Writing from/to Textual Files To write: put() writing single character << operator writing an object To read: get() reading a single character of a buffer getline() reading a single line >> operator reading a object File Handling Classes #include <fstream.h> main() { // Writing to file ofstream OutFile("my_file.txt"); OutFile<<"Hello "<<5<<endl; OutFile.close(); int number; char dummy[15]; // Reading from file ifstream InFile("my_file.txt"); InFile>>dummy>>number; } InFile.seekg(0); InFile.getline(dummy, sizeof(dummy)); InFile.close();