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

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

Random File Access. 1. Random File Access

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.

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++ Programming Lecture 10 File Processing

Unit-V File operations

More File IO. CIS 15 : Spring 2007

IS 0020 Program Design and Software Tools

C++ files and streams. Lec 28-31

Developed By : Ms. K. M. Sanghavi

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

Fall 2017 CISC/CMPE320 9/27/2017

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

Lecture 9. Introduction

by Pearson Education, Inc. All Rights Reserved. 2

Fundamentals of Programming Session 27

Study Material for Class XII. Data File Handling

Object Oriented Programming In C++

Advanced I/O Concepts

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

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

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

Stream States. Formatted I/O

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

ios ifstream fstream

Chapte t r r 9

UNIT V FILE HANDLING

Object Oriented Programming

Chapter 8 File Processing

Lecture 5 Files and Streams

Streams contd. Text: Chapter12, Big C++

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

Introduction. Lecture 5 Files and Streams FILE * FILE *

========================GDS2_BIN2TXT_main.cpp============================================

Module 11 The C++ I/O System

C++ How to Program 14.6

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

File Processing in C++

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

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

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

Chapter 14 Sequential Access Files

Writing a Good Program. 7. Stream I/O

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

Input and Output File (Files and Stream )

Object Oriented Programming CS250

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

MANAGING FILES OF RECORDS

EP241 Computing Programming

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

Fundamentals of Programming Session 28

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

Streams - Object input and output in C++

Fundamental File Processing Operations 2. Fundamental File Processing Operations

BITG 1113: Files and Stream LECTURE 10

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

Conceptually a file is thought of as an array of bytes. If there are k bytes in the file, the bytes are indexed from 0 to k-1.

THAO TÁC VỚI TẬP TIN TRONG C++

Page 1

Strings and Stream I/O

Generate error the C++ way

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

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

DISK FILE PROGRAM. ios. ofstream

10/23/02 21:20:33 IO_Examples

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

Input/Output Streams: Customizing

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

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

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

CS201 Latest Solved MCQs

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

System Design and Programming II

UNIT V FILE HANDLING

C++ Structures Programming Workshop 2 (CSCI 1061U)


COMP322 - Introduction to C++

by Pearson Education, Inc. All Rights Reserved. 2

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

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

A <Basic> C++ Course

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

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

Lecture 14. Xiaoguang Wang. March 11th, 2014 STAT 598W. (STAT 598W) Lecture 14 1 / 36

A <Basic> C++ Course

C++ Input/Output: Streams

CS201 Solved MCQs.

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

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

JB Academy, Faizabad Half Yearly Examination Subject: Computer Science (083) Class XII

(1)Given a binary file PHONE.DAT, containing records of the following structure type class Phonlist { char Name[20]; char Address[30]; char

Fundamentals of Programming Session 25

Applications with Files, Templates

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

Downloaded from

Transcription:

C++ Binary File I/O C++ file input and output are typically achieved by using an object of one of the following classes: ifstream for reading input only. ofstream for writing output only. fstream for reading and writing from/to one file. To define any of the above classes, you need to include <fstream> package, which includes those classes. Caution: For binary file I/O you do not use the conventional text-oriented << and >> operators! It can be done, but that is an advanced topic. We will discuss the easy way here. Basic Model for File I/O The file stream classes are simply be viewed as a stream or array of uninterpreted bytes. That is, it is like an array of bytes stored and indexed from zero to len-1, where len is the total number of bytes in the entire file. Each open file has two positions associated with it:. 1. The current reading position, which is the index of the next byte that will be read from the file. It simply points to the next character that the basic get method will return. 2. The current writing position, which is the index of the byte location where the next output byte will be placed. It simply points to the location where the basic put method will place byte(s). Repositioning with the get and put stream pointers All i/o streams objects have, at least, one internal stream pointer: For input streams like, ifstream and istream, you use get to move the pointer that points to the element to be read in the next input operation. For output streams, like ofstream and ostream, you use the put pointer that points to the location where the next element has to be written. Recall that fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both istream and ostream).

These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions: tellg() and tellp() These two member functions have no parameters They return a value of the member type pos_type, which is an integer data type representing the current position of the get stream pointer (in the case of tellg) or the put stream pointer (in the case of tellp). seekg() and seekp() You may change the position of the get and put stream pointers using seekg() and seekp() functions. Both functions are overloaded with two different prototypes. The first prototype is: seekg ( position ); seekp ( position ); With this prototype the stream pointer is changed to the absolute position position (counting from the beginning of the file). The type for this parameter is the same as the one returned by functions tellg and tellp: the member type pos_type, which is an integer value. The other prototype for these functions is: seekg ( offset, direction ); seekp ( offset, direction ); Using this prototype, the position of the get or put pointer is set to an offset value relative to some specific point determined by the parameter direction. offset is of the member type off_type, which is also an integer type. And direction is of type seekdir, which is an enumerated type (enum) that determines the point from where offset is counted from, and that can take any of the following values:

ios::beg offset counted from the beginning of the stream offset counted from the current position of the stream ios::cur pointer ios::end offset counted from the end of the stream The following example uses the member functions we have just seen to obtain the size of a file: // obtaining file size long begin,end; ifstream myfile ("example.txt"); begin = myfile.tellg(); myfile.seekg (0, ios::end); end = myfile.tellg(); myfile.close(); cout << "size is: " << (end-begin) << " bytes.\n"; Opening a File A file stream object can be opened in one of two ways. ifstream myfile ( data.bin, ios::in ios::binary); Or, after a file stream object has been declared, you can call its open method: ifstream myfile;... myfile.open ("data.bin", ios::in ios::binary); Either approach will work with an ifstream, an ofstream, or an fstream object. For example, you may declare an output file as following: ofstream myfile;...

myfile.open ("data2.bin", ios::out ios::binary); When manipulating text files, one omits the second parameter (the i/o mode parameter). However, in order to manipulate binary files, you should always specify the i/o mode, including ios::binary as one of the mode flags. For read/write access to a file, use an fstream: fstream myfile; myfile.open ("data3.bin", ios::in ios::out ios::binary); Reading and Writing to a Binary File Read and Write are special functions for binary files. o write is a member function of ostream inherited by ofstream. o read is a member function of istream that is inherited by ifstream. Objects of class fstream have both members. Their prototypes are: o write ( memory_block, size ) o read ( memory_block, size ); o Where memory_block is of type pointer to char (char*) (for the time consider this as an array), and represents the address of an array of bytes where the read data elements are stored or from where the data elements to be written are taken. o The size parameter is an integer value that specifies the number of characters to be read or written from/to the memory block. // reading a complete binary file ifstream::pos_type size; char * memblock; ifstream file ("example.txt", ios::in ios::binary ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close();

cout << "the complete file content is in memory"; delete[] memblock; else cout << "Unable to open file"; In this example the entire file is read and stored in a memory block. Let's examine how this is done: First, the file is open with the ios::ate flag, which means that the get pointer will be positioned at the end of the file. This way, when we call to member tellg(), we will directly obtain the size of the file. Notice the type we have used to declare variable size: ifstream::pos_type size; ifstream::pos_type is a specific type used for buffer and file positioning and is the type returned by file.tellg(). This type is defined as an integer type, therefore we can conduct on it the same operations we conduct on any other integer value, and can safely be converted to another integer type large enough to contain the size of the file. For a file with a size under 2GB we could use int: int size; size = (int) file.tellg(); Once we have obtained the size of the file, we request the allocation of a memory block large enough to hold the entire file: memblock = new char[size] Right after that, we proceed to set the get pointer at the beginning of the file (remember that we opened the file with this pointer at the end), then read the entire file, and finally close it: file.seekg (0, ios::beg); file.read (memblock, size); file.close(); At this point we could operate with the data obtained from the file. Our program simply announces that the content of the file is in memory and then terminates. Closing a File For all file stream objects, use: myfile.close();

Example 1: This program reads the whole file as binary and dumps it to screen. Hence, make sure that your input file is a text file. Otherwise, you will not see meaning full characters on the screen. // reading a complete binary file int block =100; int size, iteration, reminder; char memblock[101]; // character array ifstream file ("example.txt", ios::in ios::binary ios::ate); if (file.is_open()) { file.seekg (0, ios::end); //seek to the end size = file.tellg(); //tell the location of get pointer, ie. Find the size file.seekg (0, ios::beg); // put the get pointer to the beging of the file. iteration= size /block; reminder = size % block; while ( iteration > 0){ file.read (memblock, block); // read the whole file. memblock[block]='\0'; // append a null character to the end. // This makes the char array a C string. // If you do not, some garbage characters may be printed. cout << memblock ; //memblock[0]='\0'; //cout << endl<<endl; iteration--; //end while if (reminder > 0) { //memblock[0]='\0'; file.read (memblock, reminder); // read the whole file. memblock[reminder]='\0'; // append a null character to the end. // This makes the char array a C string. // If you do not, some garbage characters may be printed. cout << memblock ; //end if cout <<endl; cout << "Done writing the file to screen. Good Bye \n"; else cout << "Unable to open file" << endl;

Example 2; Reading the whole file and displaying on terminal at once. Again keep in mind that since you are displaying on terminal, you should input a text file. // reading a complete binary file int size; char *memblock; // pointer to character ifstream file ("example.txt", ios::in ios::binary ios::ate); if (file.is_open()) { file.seekg (0, ios::end); //seek to the end size = file.tellg(); //tell the location of get pointer, ie. size memblock = new char [size+1]; // get size +1 charter space. +1 space is for '\0' file.seekg (0, ios::beg); // put the get pointer to the begging. file.read (memblock, size); // read the whole file. file.close(); memblock[size]='\0'; // append a null character to the end. // This makes the char array a C string. // If you do not, some garbage characters may be printed. cout << "the complete file content is in memory. Here is what I read\n"; cout << memblock << endl; delete[] memblock; else cout << "Unable to open file"; Exercise: 1) Write a program that will take an executable file, and duplicate it. In addition your program should display the size of the file on the terminal. 2) Modify the program given in example 1 such that it only prints approximately 50% of the file. It should skip 25% of the file at the beginning and 25% at the end.