Fundamental file structure concepts - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Fundamental file structure concepts

Description:

Mary Ames 123 Maple Stillwater, OK 74075. After writing it ... Reading delimited files. Overload operator . Use istream ... Delimit fields with | FirstName ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 18
Provided by: cse54
Category:

less

Transcript and Presenter's Notes

Title: Fundamental file structure concepts


1
Chapter 4
  • Fundamental file structure concepts

2
Field and record organization
  • Basic unit of data is a field
  • A list of different fields is a record

3
Stream file
  • writstrm.cpp in appendix D
  • Mary Ames 123 Maple Stillwater, OK 74075
  • After writing it may look like
  • AmesMary123 MapleStillwaterOK74075
  • How can we read it back?

4
Method 1
  • Fix the length of the fields
  • Wastes space or
  • Data does not fit
  • Can be used if the data is fixed length

5
Method 2
  • Begin each field with a length indicator
  • If the fields are not longer than 255 you can use
    one byte to store the length

6
Method 3
  • Separate the fields with delimiters
  • Use a delimiter that is not in the data
  • White space is often in the data
  • This is the method we will use in the examples

7
Method 4
  • Use keywordvalue expression
  • Self-describing
  • Good if fields are missing
  • Still need delimiter in some cases
  • Wastes space

8
Reading delimited files
  • Overload operatorgtgt
  • Use istream method getline
  • Reads up to the first occurrence of the delimiter
    or the end-of-line
  • See readdel.cpp in appndx D

9
Record structures
  • A set of fields that belong together
  • Saving the state of an object stored in memory
  • How to organize records of a file

10
Method 1
  • Fixed length records
  • Most commonly used
  • Fixed length records does not imply fixed length
    fields

11
Method 2
  • Fix the number of fields
  • For example, 6 fields as in the Person

12
Method 3
  • Begin each record with a length indicator
  • Common for variable length records

13
Method 4
  • Use an index
  • Byte offset for each record in the file

14
Method 5
  • Delimiter at the end of each record
  • Same as for fields
  • Common is end-of-line

15
Example record structure
  • Use length indicator at the beginning of a record
  • Accumulate the record in a buffer
  • Use strcat to put the strings in the buffer
  • Use strlen to calculate the length
  • Write the buffer length and the buffer

16
Representing the record length
  • 2-byte binary integer or
  • Convert to char string

17
project
  • Write a program that prompts for the three data
    items and writes them to a file. Use variable
    length records with the length stored in binary
    at the start of each record. Delimit fields with
  • FirstNameLastnametelno
Write a Comment
User Comments (0)
About PowerShow.com