random access files lesson xx

20
Random Access Files Lesson xx

Upload: kura

Post on 22-Feb-2016

60 views

Category:

Documents


0 download

DESCRIPTION

Random Access Files Lesson xx. Objectives. Random access vs. sequential access Current position pointer (CP) Record numbers seekg command tellg command Program using random access files. Sequential vs. Random Access Files . - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

Random Access Files Lesson xx

In this module, well talk about random access files.1ObjectivesRandom access vs. sequential accessCurrent position pointer (CP) Record numbers seekg commandtellg commandProgram using random access files

Our goal is cover the following topics regarding files: random access vs sequential files, the current position pointer, record numbers, seekg command, tellg command and finally, well write a program using random access files.2Sequential vs. Random Access Files Sequential file In order to read in record # 99, you must read records # 1- 98 first

Random Access You may directly read record #n without having to read the first n -1 records.

In a random access file, record size is a fixed length

Random access files must be written in binary mode

seekg controls the current position pointer

Up to this point, we have been dealing with sequential files. This means that in order to read record 99, you must first read records 1- 98. This process takes a long time if you need to read in record # 1 million. A random access file allows you to go directly to a specific record. The records on a random access files are a fixed length and must be written in binary. You can go to any record you wish by moving the current position pointer using the seekg function.3Program Description Use the binary file, test.dat, that was created in the previous module

Calculate the # of records that are stored on the file

Prompt the to enter a legal record # and display the record.

Repeat step 3 until the user enters an illegal record #, at this time, terminate the program.

Lets demonstrate random access files by writing the following program. 1) Well use the binary file, test.dat that was created in the previous module. 2) Well calculate the # of records stored on the file. 3) In a loop, well prompt the user to enter a record # and then display the record. 4) Step 3 is repeated until the user enters an illegal record #. At that time, we exit the loop.

4Data File test.dattest.datJoe Blow 99 98 97Sam Spade 88 66 42Art Hom 39 99 88Ann Hsu 76 45 23Ada Lo 50 34 99

We can picture the file test.dat as shown in the slide. In real life, this is not what it looks like. The data is stored in binary mode and very difficult to read.5Program Listing Part 1#include using std::ifstream; #include using std::cin; using std::cout; using std::endl; using std::ios; #include struct student { char name[25]; int grade[3]; }; int main() { student s; long recno; char recstr[10]; ifstream fin; fin.open("test.dat", ios::binary);

Well list the entire program for you 1st and then go through all the details later. This is part 1 of the program which has the preprocessor directives, the structure definition and the various declarations.6Program Listing Part 2 fin.seekg(0, ios::end); // go to end of file long lastByte = fin.tellg(); // get byte# int n = lastByte / sizeof(student); // #of records cout