Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Random access vs. sequential access  Current position pointer (CP)  Record numbers  seekg command  tellg command  Program using random access.

Similar presentations


Presentation on theme: " Random access vs. sequential access  Current position pointer (CP)  Record numbers  seekg command  tellg command  Program using random access."— Presentation transcript:

1

2

3  Random access vs. sequential access  Current position pointer (CP)  Record numbers  seekg command  tellg command  Program using random access files

4 1.Sequential file – In order to read in record # 99, you must read records # 1- 98 first 2.Random Access – You may directly read record #n without having to read the first n -1 records. 3.In a random access file, record size is a fixed length 4.Random access files must be written in binary mode 5.seekg controls the current position pointer

5 1. Use the binary file, test.dat, that was created in the previous module 2. Calculate the # of records that are stored on the file 3.Prompt the to enter a legal record # and display the record. 4.Repeat step 3 until the user enters an illegal record #, at this time, terminate the program.

6 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

7 #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);

8 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 > recstr; recno = atoi(recstr); if ((recno > 0) && (recno <= n)) { long offset = (recno - 1) * sizeof(student); fin.seekg(offset); fin.read((char*)&s, sizeof(student));

9 cout << s.name; int i; for (i = 0; i < 3; i++) cout << " " << s.grade[i]; cout << endl; } else break; } while(1); fin.close(); return 0; }

10 #include using std::ifstream; #include using std::cin; using std::cout; using std::endl; using std::ios; #include struct student { char name[25]; int g[3]; };

11 int main() { student s; long recno; char recstr[10]; ifstream fin; fin.open("test.dat", ios::binary);

12 1. Place the current position pointer (CP) at the end of file 2.Use the tellg command to see what byte # the CP is on 3.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.

13 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 CP 0-19 recoord 1 20-39 record 2 40-59 recprd 3 60-79 record 4 80-99 record 5 100 EOF

14 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";

15 do { cout > recstr; recno = atoi(recstr);

16 if ((recno > 0) && (recno <= n)) { long offset = (recno - 1) * sizeof(student); fin.seekg(offset); fin.read((char*)&s, sizeof(student)); cout << s.name; int i; for (i = 0; i < 3; i++) cout << " " << s.grade[i]; cout << endl; }

17 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 CP 0-19 recoord 1 20-39 record 2 40-59 recprd 3 60-79 record 4 80-99 record 5 100 EOF long offset = (recno - 1) * sizeof(student); fin.seekg(offset); fin.read((char*)&s, sizeof(student)); Sam Spade s.name 88 66 42 s.g[0] s.g[1] s.g[2] s

18 else break; } while(1); fin.close(); return 0; }

19 from where fin.seekg (5, ios::beg); # bytes to move

20 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

21 Random access vs. sequential access Current position pointer (CP) Record numbers seekg command tellg command Program using random access files


Download ppt " Random access vs. sequential access  Current position pointer (CP)  Record numbers  seekg command  tellg command  Program using random access."

Similar presentations


Ads by Google