Random Access File
4 The Random Access File 5 Sequential Operations 6 Computing the number of records 7 Sequential Listing of Records 8 Random Record Access Defining a Record Structure To facilitate easy random access, it is necessary that all of the "records" in the file are the same size. This will allow us to locate a specific record easily by performing some simple math. We need a representation of a campsite with the following values:site number (an integer will work for this) *site description (a string) * a flag indicating whether the site provides electric power or nota per-night booking rate for the site (a floating point value will work for this) We want to keep the record simple, so we will use a struct. Create a header file CampsiteRecord.h and add the following structure definition * A fixed-size record representing a campsite in a * recreational area struct CampsiteRecord CampsiteRecord ( CampsiteRecord(int number, const char description[ , bool has_electric, double rate) static const int desc_size 128; int char bool double max storage size of description site number number description[ desc_sizeli IIl a short description has electric, rate: /Iis power available? /IT per-night rate The non-default constructor should copy its parameters into the attributes of the object. Implement it now, being sure to copy the c-string description safely Hint: Remember, you cannot use assignment to copy c strings!. You might want to refer to your notes, book, or online documentation for a refresher on how to use strnepy from the
library. Implement the constructor in a new implementation file CampsiteRecord.cpp. Avoid information leakage! There is a security concern that we must mention at this point. The random access file will be written in binary mode, meaning that all of the bytes contained within the record will be copied directly into the file. That would include any bytes that are not actually in use in the record-the ones at the end of the description c-string, and potentially '"padding" bytes introduced between attributes in physical memory (this varies from system to system). But, those "unused" bytes might actually contain information... Remember that main memory isn't "cleared when variables go out of scope values often remain there until the memory is re-used. Writing from "unused" memory into a file leaks whatever information was in that unused memory. Imagine the following scenario, where the description is drastically shortened to it on the screen (it is not shown at its real 128 byte length, instead it is shown as 24 bytes). The bytes marked n belong to number, d belong to description, e belong to has electric, r belong to rate Memory before a record is created (i.e. "uninitialized): my password is open sesame". Don't t Memory after a record has been created (the represents the terminating 0' on the c-string): 4 The Random Access File 5 Sequential Operations 6 Computing the number of records 7 Sequential Listing of Records 8 Random Record Access Defining a Record Structure To facilitate easy random access, it is necessary that all of the "records" in the file are the same size. This will allow us to locate a specific record easily by performing some simple math. We need a representation of a campsite with the following values:site number (an integer will work for this) *site description (a string) * a flag indicating whether the site provides electric power or nota per-night booking rate for the site (a floating point value will work for this) We want to keep the record simple, so we will use a struct. Create a header file CampsiteRecord.h and add the following structure definition * A fixed-size record representing a campsite in a * recreational area struct CampsiteRecord CampsiteRecord ( CampsiteRecord(int number, const char description[ , bool has_electric, double rate) static const int desc_size 128; int char bool double max storage size of description site number number description[ desc_sizeli IIl a short description has electric, rate: /Iis power available? /IT per-night rate The non-default constructor should copy its parameters into the attributes of the object. Implement it now, being sure to copy the c-string description safely Hint: Remember, you cannot use assignment to copy c strings!. You might want to refer to your notes, book, or online documentation for a refresher on how to use strnepy from the library. Implement the constructor in a new implementation file CampsiteRecord.cpp. Avoid information leakage! There is a security concern that we must mention at this point. The random access file will be written in binary mode, meaning that all of the bytes contained within the record will be copied directly into the file. That would include any bytes that are not actually in use in the record-the ones at the end of the description c-string, and potentially '"padding" bytes introduced between attributes in physical memory (this varies from system to system). But, those "unused" bytes might actually contain information... Remember that main memory isn't "cleared when variables go out of scope values often remain there until the memory is re-used. Writing from "unused" memory into a file leaks whatever information was in that unused memory. Imagine the following scenario, where the description is drastically shortened to it on the screen (it is not shown at its real 128 byte length, instead it is shown as 24 bytes). The bytes marked n belong to number, d belong to description, e belong to has electric, r belong to rate Memory before a record is created (i.e. "uninitialized): my password is open sesame". Don't t Memory after a record has been created (the represents the terminating 0' on the c-string)