lecture set 12 sequential files and structures part c – reading and writing binary files

7
Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Upload: clarence-barnett

Post on 31-Dec-2015

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Lecture Set 12

Sequential Files and Structures

Part C – Reading and Writing Binary Files

Page 2: Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Slide 2

Objectives

Distinguish between a text file and a binary file.

Describe the use of BinaryReader, and BinaryWriter objects.

8/25/2013 1:19 PM

Page 3: Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Slide 3

Text vs Binary Files (review)

A text file displayed in a text editor

A binary file displayed in a text editor

8/25/2013 1:19 PM

Page 4: Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Slide 4

Binary Files – A Brief Introduction

Reading and writing to and from binary files is more efficient than processing text files since no conversion (from internal machine level representation to characters) is necessary

The fields you write are written directly, in sequence, one at a time, without conversionThe basic syntax for creating a BinaryWriter object

new BinaryWriter(stream)

Common methods of the BinaryWriter class Write(data)

Close()

8/25/2013 1:19 PM

Page 5: Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Slide 5

Code that Writes Data from a Collection of Product Objects to a Binary File

BinaryWriter binaryOut = new BinaryWriter (new FileStream(path, FileMode.Create, FileAccess.Write)) foreach (Product product in products) { binaryOut.Write(product.Code); binaryOut.Write(product.Description); binaryOut.Write(product.Price); } // end for loop binaryOut.Close();

8/25/2013 1:19 PM

Page 6: Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Slide 6

Binary Readers

The basic syntax for creating a BinaryReader object

new BinaryReader(stream)

Common methods of the BinaryReader class PeekChar()

Read()

ReadBoolean()

ReadByte()

ReadChar()

ReadDecimal()

ReadInt32()

ReadString()

Close()

8/25/2013 1:19 PM

Page 7: Lecture Set 12 Sequential Files and Structures Part C – Reading and Writing Binary Files

Slide 7

Binary Readers (continued)

Code that reads data from a binary file into a collection of Product objects

BinaryReader binary = new BinaryReader (new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); List<Product> products = new List<Product>; while (binaryIn.PeekChar != -1) { Product product = new Product; product.Code = binaryIn.ReadString; product.Description = binaryIn.ReadString; product.Price = binaryIn.ReadDecimal; products.Add(product); } // end while loop binaryIn.Close();

8/25/2013 1:19 PM