200 likes | 536 Views
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 .
E N D
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 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
Program 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.
Data File test.dat test.dat Joe Blow 99 98 97 Sam Spade 88 66 42 Art Hom 39 99 88 Ann Hsu 76 45 23 Ada Lo 50 34 99
Program Listing Part 1 #include <fstream> using std::ifstream; #include <iostream> using std::cin; using std::cout; using std::endl; using std::ios; #include <cstdlib> 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);
Program 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 << "Currently there are " << n << " records.\n"; do { cout << "\nWhich record do you wish to display? "; cin >> recstr; recno = atoi(recstr); if ((recno > 0) && (recno <= n)) { long offset = (recno - 1) * sizeof(student); fin.seekg(offset); fin.read((char*)&s, sizeof(student));
Program Listing Part 3 cout << s.name; inti; for (i = 0; i < 3; i++) cout << " " << s.grade[i]; cout << endl; } else break; } while(1); fin.close(); return 0; }
Preprocessor Directives and Structure Definition #include <fstream> using std::ifstream; #include <iostream> using std::cin; using std::cout; using std::endl; using std::ios; #include <cstdlib> struct student { char name[25]; int g[3]; };
Declarations int main() { student s; long recno; char recstr[10]; ifstream fin; fin.open("test.dat", ios::binary);
How to Calculate # of Records in a File Place the current position pointer (CP) at the end of file Use the tellg command to see what byte # the CP is on Take the byte # from step 2 and divide it by the size of the structure. This will give you the # of records on the file.
Current Position Pointer (CP) test.dat Joe Blow 99 98 97 Sam Spade 88 66 42 Art Hom 39 99 88 Ann Hsu 76 45 23 Ada Lo 50 34 99 0-19 recoord 1 20-39 record 2 40-59 recprd 3 60-79 record 4 80-99 record 5 100 EOF CP
Calculate # of Records fin.seekg (0, ios::end); // go to end of file long lastByte = fin.tellg(); // get byte# int n = lastByte / sizeof(student); // #of records cout << "Currently there are " << n << " records.\n";
Ask User for Input do { cout << "\nWhich record do you wish to display? "; cin >> recstr; recno = atoi(recstr);
Read in a Legal Record if ((recno > 0) && (recno <= n)) { long offset = (recno - 1) * sizeof(student); fin.seekg(offset); fin.read((char*)&s, sizeof(student)); cout << s.name; inti; for (i = 0; i < 3; i++) cout << " " << s.grade[i]; cout << endl; }
Moving CP to 1st Byte of Desired Record long offset = (recno - 1) * sizeof(student); fin.seekg(offset); fin.read((char*)&s, sizeof(student)); test.dat Joe Blow 99 98 97 Sam Spade 88 66 42 Art Hom 39 99 88 Ann Hsu 76 45 23 Ada Lo 50 34 99 0-19 recoord 1 20-39 record 2 40-59 recprd 3 60-79 record 4 80-99 record 5 100 EOF CP s Sam Spade s.name 88 s.g[0] 66 s.g[1] 42 s.g[2]
else Branch else break; } while(1); fin.close(); return 0; }
seekg Parameters from where fin.seekg (5, ios::beg); # bytes to move
Variations of fseek 1. fin.seekg (0, ios::beg); //move CP to beg. of file 2.fin.seekg (0); // move CP to beg. of file 3. fin.seekg (n); // move CP n bytes 4. fin.seekg (0, ios::end); // move CP to EOF 5. fin.seekg ( -10, ios::end); // move back 10 bytes 6. fin.seekg ( 5, ios::cur); //move CP forward 5 bytes
Summary • Random access vs. sequential access • Current position pointer (CP) • Record numbers • seekg command • tellg command • Program using random access files