File Structures in data structures.

FILE STRUCTURE:

*Indexes made it possible to keep a list of keys and pointers in a smaller file that could be searched more quickly.

*Simple indexes became difficult to manage for dynamic files in which the set of keys changes. 

*The simplest organization for a file is sequential.

*A sequential file is a sequence of records.

*The records may or may not be kept in sorted order in the sequence.

*Although file records are typically of type structure, a file record may also be declared to be of type integer, float, character, or any other C type.

*Trees grew unevenly as records were added and deleted, resulting in long searches requiring multiple disk accesses to find a record. Hence an elegant, self-adjusting binary tree structure called an AVL tree was developed for data in memory.

*Even with a balanced binary tree, dozens of accesses were required to find a record in moderate-sized files.

*A method was needed to keep a tree balanced when each node of the tree was not a single record, as in a binary tree, but a file block containing hundreds of records

*AVL trees grow from top down as records are added, B-Trees grow from the bottom up.

*B-Trees provided excellent access performance but, a file could not be accessed sequentially with efficiency.

*To further reduce the number of disk accesses, hashing was introduced for files that do not change size greatly over time.

*Extendible, dynamic hashing was introduced for volatile, dynamic files which change.

The open function must be supplied with :

  •  The name of the physical file
  •  The access mode
  •  For new files, the protection mode

The value returned by the open is the fd, and is assigned to the file variable.
Function to open a file:

  • fd = open(filename,flags[,pmode]);
  • fd-file descriptor

UNIX and PC-DOS:
For handle level access, the logical file is declared as an int.
The handle is also known as a file descriptor.
  Prototypes:

  • int open (const char* Filename, int Access);
  • int open (const char* Filename, int Access, int Protection);

Example:
int Input;
Input = open ("Daily.txt", O_RDONLY);


The following flags can be bitwise ored together for the access mode:

  • O_RDONLY : Read only.
  • O_WRONLY : Write only.
  • O_RDWR : Read or write
  • O_CREAT : Create file if it does not exist.
  • O_EXCL : If the file exists, truncate it to a length of zero, destroying itscontents. (used only with O_CREAT).
  • O_APPEND : Append every write operation to the end of the file.
  • O_TRUNC : Delete any prior file contents

Pmode- protection mode
The security status of a file, defining who is allowed to access a file, and which access modes are allowed.
 

To disassociate a logical program file from a physical system file.

  •  Closing a file frees system resources for reuse
  • .Data may not be actually written to the physical file until a logical file is closed.
  • A program should close a file when it is no longer needed.
  • The C++ close function is used to close a file for handle level access.
  • The handle close function must be supplied with (as an argument):

 The handle of the logical file
The value returned by the close is 0 if the close succeeds, and -1 if the close fails..
Prototypes:

  • int close (int Handle);

EXAMPLE:

  • close (Input);


 

Posted on by