Data and File Structures Chapter 4
Fundamental File Structure
Concepts
Outline
• Stream Files
• Field Structures
• Reading a Stream of Fields
• Record Structures
• Record Structures that use a length
indicator
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.
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.
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
Four methods for organizing fields
within records
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’
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.
Fixed-length Records & Fixed
number of Fields
Variable-length Records
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
JSON example
{
"id": 1,
"name": "Foo", "price": 123,
"tags": ["Bar","Eek"],
"stock": { "warehouse":300, "retail":20 }
}
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>
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;
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;
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;