1 / 20

Random Access Files Lesson xx

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 .

kura
Download Presentation

Random Access Files Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Random Access Files Lesson xx

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

  3. 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

  4. 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.

  5. 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

  6. 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);

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

  8. 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; }

  9. 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]; };

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

  11. 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.

  12. 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

  13. 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";

  14. Ask User for Input do   { cout << "\nWhich record do you wish to display? "; cin >> recstr; recno = atoi(recstr);

  15. 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;  }

  16. 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]

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

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

  19. 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

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

More Related