• 검색 결과가 없습니다.

Chapter 4

N/A
N/A
Protected

Academic year: 2021

Share "Chapter 4"

Copied!
18
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

Data and File Structures Chapter 4

Fundamental File Structure

Concepts

(2)

Outline

• Stream Files

• Field Structures

• Reading a Stream of Fields

• Record Structures

• Record Structures that use a length

indicator

(3)

Field and Record Organization:

Overview

• The basic logical unit of data is the field which contains a single data value.

• Fields are organized into aggregates, either as many

copies of a single field (an array) or as a list of different fields (a record).

• When a record is stored in memory, we call it object and call its field member.

 When that object is stored in a file, we call it simply a record.

• In this lecture, we will investigate the many ways that

objects can be represented as records in files.

(4)

Stream Files

• Ames Mary

• 123 Maple

• Stillwater, OK 74075

• Mason Alan

• 90 Eastgate

• Ada, OK 74820

• In Stream Files, the information is written as a stream of bytes containing no added information:

AmesMary123 MapleStillwaterOK74075MasonAlan90 EastgateAdaOK74820

• Problem: There is no way to get the information

back in the organized record format.

(5)

Field Structures

• There are many ways of adding structure to files to maintain the identity of fields:

– Force the field into a fixed length

• This format wastes a lot of space

– Begin each field with a length indicator

– Place a delimiter at the end of each field to separate it from the next field

– Use a “keyword = value” expression to identify each field and its content.

• It provides information about itself (self-describing structure)

• It also wastes a lot of space

(6)

Four methods for organizing fields

within records

(7)

Reading a Stream of Fields

• A Program can easily read a stream of fields and output ===>

• This time, we do

preserve the notion of fields, but something is missing:

– Rather than a stream of fields, these should be two records

Last Name: ‘Ames’

First Name: ‘Mary’

Address: ‘123 Maple’

City: ‘Stillwater State: ‘OK’

Zip Code: ‘74075’

Last Name: ‘Mason’

First Name: ‘Alan’

Address: ‘90 Eastgate’

City: ‘Ada’

State: ‘OK’

Zip Code: ‘74820’

(8)

Record Structure

• A record can be defined as a set of fields.

• Records are an important logical notion in the file’s structure.

• Most often used methods for organizing the records of a file:

– Fixed-length records.

– Fixed-number of fields.

– Beginning each record with a length indicator.

– Using a second file to keep track of the address for each record.

– Index file.

– Placing a delimiter at the end of each record to

separate it from the next record.

(9)

Fixed-length Records & Fixed

number of Fields

(10)

Variable-length Records

(11)

JSON format

• JSON (JavaScript Object Notation):

– a lightweight text-based open standard designed for human-readable data interchange

– It is derived from the JavaScript scripting language for representing simple data structures and

associative arrays, called objects

– It is often used for serializing and transmitting structured data over a network connection.

• It is used primarily to transmit data between a server

and web application, serving as an alternative to XML

(12)

JSON example

{

"id": 1,

"name": "Foo", "price": 123,

"tags": ["Bar","Eek"],

"stock": { "warehouse":300, "retail":20 }

}

(13)

XML

<person>

<firstName>John</firstName>

<lastName>Smith</lastName>

<age>25</age>

<address>

<streetAddress>21 2nd Street</streetAddress>

<city>New York</city>

<state>NY</state>

<postalCode>10021</postalCode>

</address>

<phoneNumbers>

<phoneNumber type="home">212 555-1234</phoneNumber>

<phoneNumber type="fax">646 555-4567</phoneNumber>

</phoneNumbers>

</person>

(14)

C++: Readdel.cpp:

fixed number of fields & field with a delimiter

#include <iostream>

#include <fstream>

#include <string>

#include "writeper.cpp"

using namespace std;

istream & operator >> (istream & stream, Person & p) { // read fields from file

char delim;

stream.getline(p.LastName, 30,'|');

if (strlen(p.LastName)==0) return stream;

stream.getline(p.FirstName,30,'|');

stream.getline(p.Address,30,'|');

stream.getline(p.City, 30,'|');

stream.getline(p.State,15,'|');

stream.getline(p.ZipCode,10,'|');

return stream;

(15)

C++: Readdel.cpp – cont’d

int main () {

char filename [20];

Person p;

cout << "Enter the file name:"<<flush;

cin.getline(filename, 19);

ifstream file (filename, ios::in);

if (file.fail()) {

cout << "File open failed!" <<endl;

return 0;

}

while (1) {

// read fields of person file >> p;

if (strlen(p.LastName)==0) break;

// write person to file cout << p;

(16)

C++: writeper.cpp

#include <iostream>

#include <fstream>

#include "person.h"

using namespace std;

ostream & operator << (ostream & stream, Person & p) { // insert fields into file

stream<< "Last Name '" << p.LastName <<"'\n"

<< "First Name '" << p.FirstName <<"'\n"

<< "Address '" << p.Address <<"'\n"

<< "City '" << p.City <<"'\n"

<< "State '" << p.State <<"'\n"

<< "Zip Code '" << p.ZipCode <<"'\n"

<<flush;

return stream;

(17)

Readdel.cpp – Example

(18)

Homework#2

1. Implement “Fixed-length records with variable- length fields (with a delimiter)”

– Make read_fixed_len.cpp, write_fixed_len.cpp

2. Implement “Variable length records (with a delimiter)”

– Make read_var.cpp, write_var.cpp

• Due date: 4/7

– Source code and screen capture

• Email: [email protected]

참조

관련 문서

the records have a unique identifier field or field combination called the primary

We’re going to store the service color in a model field, and we will create accessors to read and modify it, as well as trigger a color property change when modifying

9.6 Calculus Review: Functions of Several Variables 9.7 Gradients of a Scalar Field. Directional Derivative 9.8 Divergence of a Vector Field. 9.9 Curl of a Vector Field 9.9 Curl

 header includes information to determine the disk addresses of the file blocks as well as to record format descriptions, which may include field lengths and order of fields

 store value (read from the Register File during decode) written to the Data Memory.  load value, read from the Data Memory, written to

 The disk arm starts at the first I/O request on the disk, and moves toward the last I/O request on the other end, servicing requests until it gets to the other extreme

 allow instructions to contain immediate operands..  Good design

 Assuming a cache hit, the new data is written from the store buffer into the cache on the next unused cache access cycle.  Many write-back caches also include write buffers