Sequential-access file Create, read and write an object into a sequential-access file Serialize and deserialize the object to write and read from a data.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: File and Stream Tujuan Instruksional Khusus: Mahasiswa dapat.
Copyright © 2012 Pearson Education, Inc. Chapter 11 MORE WINDOWS CONTROLS & STANDARD DIALOG BOXES.
Object Oriented Programming Files and Streams Dr. Mike Spann
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
 2002 Prentice Hall. All rights reserved. 1 Chapter 17 – Files and Streams Outline 17.1 Introduction 17.2 Data Hierarchy 17.3 Files and Streams 17.4 SKIPPED.
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
File Systems Files and directories Absolute and relative names Text and binary files Sequential-access and random-access files.
ASP.NET Programming with C# and SQL Server First Edition
Creating Sequential-Access File  Serializable attribute indicates to the compiler that objects of a class can be serialized– written to or read from a.
Review of C++ Programming Part II Sheng-Fang Huang.
Windows Programming Using C# Windows Services, Serialization, and Isolated Storage.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Binary Files ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Visual C Sharp – File I/O - 1 Variables and arrays only temporary - lost when a program terminates Files used for long term storage (Data bases considered.
Chapter 12 Working with Files CIS 3260 Introduction to Programming using C# Hiro Takeda.
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Chapter 8 Data File Basics.
File I/O 11_file_processing.ppt
 2006 Pearson Education, Inc. All rights reserved Files and Streams.
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Lecture 19 Serialization Richard Gesick. Serialization Sometimes it is easier to read or write entire objects than to read and write individual fields.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.  persistent data.  The smallest.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
 2009 Pearson Education, Inc. All rights reserved. 1 Ch.19 Files and Streams Many slides modified by Prof. L. Lilien (even many without explicit message).
Object Serialization. Sequential-access Text Files Sequential-access files store records In order by the record-key field Java imposes no structure on.
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
CSC 298 Streams and files.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 21 – Files and Streams 1 Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
BIL528 – Bilgisayar Programlama II Methods 1. Contents Methods 2.
Topics Instance variables, set and get methods Encapsulation
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
18 Files and Streams.
Using Multiple Forms.
Lectures 12 Files & Streams Dr. Eng. Ibrahim El-Nahry.
Accessing Files in Java
How to work with files and data streams
CSCI 3327 Visual Basic Chapter 11: Files and Streams
Files and Streams.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
Chapter 17 – Files and Streams
Files & Streams.
Implementing Classes Chapter 3.
Object Oriented Programming
Fundaments of Game Design
CIS16 Application Development and Programming using Visual Basic.net
How to work with files and data streams
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Files and Streams.
Chapter 11 Saving Data and Objects In Files
Presentation transcript:

sequential-access file Create, read and write an object into a sequential-access file Serialize and deserialize the object to write and read from a data file The class must be defined as serializable For example, class record contain four data members

Record public __gc class Record { [Serializable] private: int account; String* firstname; String *lastname; double balance; public: ……. };

Creating Sequential-Access File Serializable attribute indicates to the compiler that objects of a class can be serialized– written to or read from a stream as objects. Objects that we wish to write to or read from a stream must include this attribute in their class definition Objects Attributes are identifiers ( within square brackets ) that specify additional information in a declaration. The code that defines an attribute is applied at runtime.

Open a file We can open files for manipulation by creating objects of classes FileStream. FileStream constructor can receive three arguments  A String * containing the name of the file to be opened  A constant describing how to open the file  A constant describing the file permission FileStream *output = new FileStream( filename, FileMode::OpenOrCreate, FileAccess::Write);

Write record to FileStream (serialize object) private: static BinaryFormatter *format = new BinaryFormatter(); format->Serialize(output, record); Output is FileStream object record is a Record object Record record;

Read from file Create FileStream to obtain read access to file private: FileStream *input; private: static BinaryFormatter *reader = new BinaryFormatter(); input = new FileStream(filename, FileMode::Open, FileAccess::Read);

Deserialize record and store data in TextBoxes try{ Record *record = dynamic_cast (reader->Deserialize(input) ); String *values[] = { record-> Account.ToString(), record->LastName->ToString(), record->FirstName->ToString(), record->Balance.ToString()}; }; SaveTextBox(values); }

catch( SerializationException *) { input->close(); … }

SaveFileDialog Create dialog box enabling user to save file SaveFileDialog *filechooser = new SaveFileDialog(); Windows::Forms::DialogResult res = filechooser->ShowDialog(); Get specified file name String filename = filechooser->FileName;

OpenFileDialog Create dialog box enabling user to open file OpenFileDialog *filechooser = new OpenFileDialog(); Windows::Forms::DialogResult res = filechooser->ShowDialog(); Exit event if clicked cancel if( res == DialogResult::Cancel) return; Get specified file name String *filename = filechooser->FileName;