Contents


Introduction to File Handling

File handling entitles the use of many data types. Usually static in nature, such as int, double, char, array[size] but dynamic inclusive like linked lists and array[size][size undefined]. Data structures like struct are also included. In fact, struct is the most common and useful data type used in files.
Essentially, when reading from or writing to a file, one must first open the file. After the file has been opened, data may be read through the file or written to it through the use of the client program accessing the file. To avoid trouble later with unclosed files, programming with files does not require, but insists that the file be closed after editing has been completed.

C++ syntax for simple file handling is done with the fstream definition.

fstream identity;
identity.open("file and destination", ios in/out); //opens file for //reading and writing
identity >> / << either data type or strings; //outputs to a file, or //draws data from a file, depending on the '>>/<<' sign
identity.close(); //file is closed


In file and Out files are established with ios::in/out and are easier file handles then other simple ones like istream and ostream.

EXAMPLE
Merging Two Lists:

//The following will merge two lists from files together
//Pseudo code used

Declare file à first, second, third
Datatype dataname1, dataname2, dataname3

While(file1 has not reached it's end OR file2 has not reached it's end)
{
open first
input dataname1 from first

open second
input dataname2 from second

if(dataname1 is proper in accordance to need)
dataname3 = dataname1
else
dataname3 = dataname2

open third
input dataname3 to third
}
close first
close second
close third

//End

 

Back to Top

Direct Access File Handling


Direct File Access, or also known as random file access employs methods of accessing the file by knowing the exact position in the file where a record is located. This is determined by a formula of essentially the following:

LOCATION = RECORD # * LENGTH OF EACH RECORD

To employ this method, each record must be placed in a section of a set length so that when accessing the file, the exact position can be known and accessed very quickly without traversing the entire list. As a result, the size of the file will probably be much larger than that of the Sequential file at the with the benefit of increased file access speed.

An example diagram of a direct / Random file access:



As you can see, all the records in the direct, or random access file are located in the same sized space. Even though R 1 may be only 2 bytes in length, it still takes up the entire 10 byte block in the file. This required for the records in the file to be accessed

Back to Top

Sequential Access File Handling

All the files you will place or find on most removable media such as floppy disks, hard disks, tape backups, etc. contain files that must be accessed through sequential file access. These kinds of files must be accessed sequentially, like a sequential search through a linked list. Every member of the file from beginning to end, like every node in the linked list, must be searched until the desired item is found. An example of this would be like searching through a document saved as a text file for a particular string and positioning file editing position at that spot.
Sequentially accessed files may have records within them of any length only restricted by the size of the medium they are placed on. This means that sequentially accessible files, though more difficult to traverse through, are the most efficient spacewise.

An example of a sequential file:



The Sequential File, as you can see, may have many different sized records. In this example, some records are 1 byte, one is 10 bytes, and there are others of varying sizes. This, as can be seen can be very efficient for disk space conservation. Next you will see how this differs with Direct File Access and how direct files differ.

C++ Code example of making a Sequential File Access:

do{
in_file >> temp;
}while(temp != '|'); //advances to Start of Name

in_file >> temp;
while(temp != '|'){
name += temp;
in_file >> temp;
}

Back to Top

Comments and Suggestions

Please tell me what you think about the page and any suggestions for it.

Comments:


From:


Back to Top

Try enteringthe Com Sci Gate for more information!

 

Copyright Sheldon - Martin Production
Last revised: April 22, 1999.