Prog+Problem Solving III
Prog+Problem Solving III CECS 277
Popular in Course
Popular in Computer Science and Engineering
This 8 page Class Notes was uploaded by Zackary Cronin on Monday October 5, 2015. The Class Notes belongs to CECS 277 at California State University - Long Beach taught by Staff in Fall. Since its upload, it has received 16 views. For similar materials see /class/218754/cecs-277-california-state-university-long-beach in Computer Science and Engineering at California State University - Long Beach.
Reviews for Prog+Problem Solving III
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/05/15
FIELD STRUCTURES Four of the most common methods of delimiting fields within a record see fig 43 in handout a b C d Force the fields into a predictable length Begin each field with a length indicator Place a delimiter at the end of each field to separate it from the next field Use a quotkeyword valuequot expression to identify each field and its contents Method 1 Fix the length of fields fig 43a a b 0 Each field has a fixed length associated with it though different fields can have different lengths The program using the file usually specifies the length of each field Disadvantages i The fixedfield approach to structuring data is often inappropriate for data that inherently contain a large amount of variability in the length of fields such as names and addresses a Field lengths may be too short to accommodate all of the pertinent information or too long causing a waste of space b Field lengths may be longer than needed thus wasting space Advantages i When reading the file you know exactly how many bytes to read in per field ii If every field is already fixed in length or if there is little variation in field lengths using a file structure consisting of a continuous stream of bytes organized into fixedlength fields is often a good solution Method 2 Begin each field with a length indicator referred to as length based fields fig 43b a b Store the field length just ahead of the field i If the fields are not too long length less than 256 bytes it is possible to store the length in a single byte at the start of each field using hex notation Advantages i Less wasted space because the only extra space used is for the field length ii Program knows exactly how many bytes to read in Method 3 Separate the fields with delimiters fig 43c a b Choose some special character or sequence of characters that will not appear within a field and then insert that delimiter into the file after writing each field Possible delimiters i White space characters a Blank b New line c Tab ii The vertical bar character Method 4 Use a quotKeyword Valuequot expression to identify fields referred to as a selfdescribing structure fig 43d a b 0 Each field is preceded with a unique keyword such as FIRSTNANIE followed by the field data i If the keyword is missing the corresponding field is assumed to be missing ii Sometimes for clarity s sake the fields are also separated by delimiters Advantages i Each field provides information about itself39 therefore it is easy to tell what fields are contained in a file ii It is easy to determine what fields are missing because the keyword is missing Disadvantage i It wastes a lot of space RECORD STRUCTURES A record can be defined as a set of fields that belong together when the file is viewed in terms of a higher level of organization Some of the most often used methods for organizing a file into records are fig 45 and fig 46 a b C d 6 Make records a predictable number of bytes fixedlength records i Make all fields a fixed length and have a fixed number of fields giving a fixed record length a Note It is also possible to have fixedlength records contain variable numbers of variablelength fields and maybe some fixedlength fields as long as all records have the same length ii Fixedlength records are probably the most common method Make records a predictable number of fields i Fields are separated by delimiters and records may be of variable lengths Begin each record with a length indicator i Each record begins with a field containing an integer that indicates how many bytes there are in the rest of the record ii This is a commonly used method for handling variablelength records Use a second file an index file to keep track of addresses i This is a twofile mechanism consisting of an index file and a data file ii We can use an index to keep a byte offset for each record in the original file iii The byte offsets allow us to find the beginning of each successive record and also let us compute the length of each record iv We look up the position of a record in the index and then seek to the record in the data file Place a delimiter at the end of each record i Similar to using delimiters to separate fields ii Common record delimiters for files that contain readable text a Endofline character 1 Carriage returnnew line pair 2 On UNIX systems just a newline character n will suffice b character Record structures that use a length indicator at the beginning of the record a b C d e To put a length indicator at the beginning of each record the sum of the field lengths must be determined before writing the record Representing the record length i Series of ASCII characters can be lengthy ii Twobyte binary integer 32767 in ASCII vs 99 in binary Person class declaration const int MaxBufferSize 200 class Person friend istreamamp operatorgtgtistreamamp stream Personamp p friend ostreamamp operatorltlt0streamamp stream const Pers0namp p public int ReadVariablePersonistreamamp stream int WritePersonostreamamp stream const private char lastl 1 char firstl 1 char addressl6 char cityl6 char state 3 char zip10 Example of a person record that contains 40 bytes using ASCII representation for the length fig 48 Example of a person record that contains 40 bytes using a 2byte integer to store length as a binary value fig 410 i Note that 28 base 16 is 40 base 10 ii Unix represents the 2byte value as 0028 A PC using MS DOS represents the value as 2800 Depending on the platform the values may be reversed This also applies to 4byte integers Writing the variable length record to the file using binary representation for length int Person WritePers0n0streamamp stream char bufferMaxBufferSize strcpybuffer last strcpybuffer l39 strcpybuffer first g strcpybuffer l39 strcpybuffer address strcpybuffer l39 strcpybuffer city strcpybuffer l39 strcpybuffer state strcpybuffer l39 strcpybuffer zip strcpybuffer l39 short length strlenbuf fer streamwriteamplength sizeoflength write length streamwriteampbuffer length Reading the variable length record from the file using binary representation for length 1 Note that istrstream input string stream is a type of input stream that uses the same operators as other input streams but has its value stored in a character string instead of in a file int Person ReadVariablePersonistreamamp stream short length streamreadamplength sizeoflength char buffer new charlength l streamreadbuf fer length bufferlength 39039 istrstream strbulTbuffer strbuff gtgt this return 1 terminate buffer with null terminator create a string stream use the istream extraction operator istreamamp operatorgtgtistreamamp stream Personamp p streamgetlineplast 11 l if strlenplast 0 return stream streamgetlinep rst 11 39l39 streamgetlinepaddress 16 l39 streamgetlinepcity 16 39l streamgetlinepstate 3 l streamgetlinepzip 10 39l39 return stream 4 Methods to create view and update a file that contains binary information a Visual Studio 60 1 ii in File creation a Choose File New Binary File View and update a If the file has unprintable characters as with binary data when you choose File Open the file will be opened using the binary editor Updating a The display is grouped into 3 vertical segments 1 The leftmost segment is the byte offset 2 The middle segment is a hex display of the file You can type values into this area but they must be hex values 3 The rightmost segment shows any characters that can be displayed If you type ASCII characters in this area they will be translated into hex values in the middle segment b You can move between the middle and last segments using the tab key b Visual Studio NET and NET 2003 1 ii Unlike Visual Studio 60 there isn t an option to select File New Binary Visual Studio NET will not invoke the binary editor unless the file contains unprintable characters or has a bin extension a You can create a quotdummyquot binary file by opening Notepad creating a file with some bogus data then saving the file with a bin extension choose quotAll Filesquot in the quotSave as typequot textbox then type bin at the end of the filename Then you can edit the file in Visual Studio NET s binary editor and later change the extension to whatever you want Viewing and updating are similar to Visual Studio 60 except NET will open a file with a bin extension in the binary editor as well as a file that contains unprintable characters Visual Studio 60 will only open the file in the binary editor if it contains unprintable characters INHERITAN CE IN THE C STREAM CLASSES C incorporate inheritance to all multiple classes to share members and methods a Read operations including the extraction operators are defined in class istream b Write operations are defined in class ostream c Open and close operations of class fstreambase are also members of class fstream The following definitions are included in header files iostream and fstream class istream Virtual public ios class ostream Virtual public ios class iostream public istream public ostream class ifstream public fstreambase public istream class ofstream public fstreambase public ostream class fstream public fstreambase public iostream BIBLIOGRAPHY Folk Michael J Bill Zoellick and Greg Riccardi File Structures An Object Oriented Approach with C Reading Massachusetts AddisonWesley 1998
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'