Title: IO and File Manipulations: Random Access File Processing
1I/O and File ManipulationsRandom Access File
Processing
Wow.. I dont know programming is much easier
than I thought it before. Yes! I can do it.. ?
2You will learn
- Sequential file vs. random access file
- File pointer indicators
- seekp(), seekg(), tellp(), tellg()
- Example of random access file
- write(), read()
3Recap about sequential access file
- Data records are written to, or read from, the
file in a sequential manner
4Updating Sequential-Access Files
- When we would like to update sequential files
- Risk overwriting other data
- Example change name "White" to "Worthington"
- Old data
- 300 White 0.00 400 Jones 32.87
- Insert new data
5Can you see what ??
6The solution is to use random access file!!!
7Random Access means..
- Moving directly to any location in the file,
instead of moving through it sequentially.. - Any program will
- Compartmentalize the file (storage space) into
equi-partition spaces - maintain an indexing method, which will determine
the location of data compartment in the file - Hence, it makes sense to jump directly to that
location, read and/or modify it
8Of many techniques to create random access files
- Making all the records in a file of same fixed
length is the easiest technique! - Goodness
- Make it easy for a program to calculate the exact
location of any record relative to the beginning
of the file
9File I/O utilities
- I/O processes are invisible to users
- How can we know that a process runs smoothly?
- Need to monitor the stream condition continuously
and autonomously
10Stream Error States
- Test state of stream using bits
- eofbit set when EOF encountered
- Function eof returns true if eofbit set
- cin.eof()
- failbit set when error occurs in stream
- Data not lost, error recoverable
- Function fail returns true if set
- badbit set when data lost
- Usually nonrecoverable
- Function bad
- goodbit set when badbit, failbit and eofbit off
- Function good
11Stream Error States (contd)
- Member functions
- rdstate()
- Returns error state of stream
- Can test for goodbit, badbit, etc.
- Better to test using good(), bad()
- clear()
- Default argument goodbit
- Sets stream to "good" state, so I/O can continue
- Can pass other values
- cin.clear( iosfailbit )
- Sets failbit
- Name "clear" seems strange, but correct
121 // Adapted from Deitels and Deitels text
book 2 // Testing error states. 3
include ltiostreamgt 4 5 using
stdcout 6 using stdendl 7 using
stdcin 8 9 int main() 10 11
int integerValue 12 13 // display
results of cin functions 14 cout ltlt "Before
a bad input operation" 15 ltlt
"\ncin.rdstate() " ltlt cin.rdstate() 16
ltlt "\n cin.eof() " ltlt cin.eof() 17
ltlt "\n cin.fail() " ltlt cin.fail() 18
ltlt "\n cin.bad() " ltlt cin.bad()
19 ltlt "\n cin.good() " ltlt
cin.good() 20 ltlt "\n\nExpects an
integer, but enter a character " 21 22
cin gtgt integerValue // enter character value 23
cout ltlt endl 24
1325 // display results of cin functions
after bad input 26 cout ltlt "After a bad
input operation" 27 ltlt
"\ncin.rdstate() " ltlt cin.rdstate() 28
ltlt "\n cin.eof() " ltlt cin.eof() 29
ltlt "\n cin.fail() " ltlt cin.fail() 30
ltlt "\n cin.bad() " ltlt cin.bad()
31 ltlt "\n cin.good() " ltlt
cin.good() ltlt endl ltlt endl 32 33
cin.clear() // clear stream 34 35 //
display results of cin functions after clearing
cin 36 cout ltlt "After cin.clear()" 37
ltlt "\ncin.fail() " ltlt cin.fail() 38
ltlt "\ncin.good() " ltlt cin.good() ltlt endl 39
40 return 0 41 42 // end main
14Before a bad input operation cin.rdstate() 0
cin.eof() 0 cin.fail() 0 cin.bad() 0
cin.good() 1 Â Expects an integer, but enter a
character A Â After a bad input
operation cin.rdstate() 2 cin.eof() 0
cin.fail() 1 cin.bad() 0 cin.good()
0 Â After cin.clear() cin.fail() 0 cin.good() 1
15Sequential File VS Random Access File
16To create, write and read random access files .
- Random access files requires agreement between
programs used to write and read the files - Tools are necessary to enable random access
within the file
17About file position indicator (FPI)/file
position pointer (FPP)
- This indicator/pointer is used to determine the
position in a file at which the next read or
write operation will occur - This helps in both sequential and random access
files - 2 types of FPI
- For reading (get pointer)
- For writing (put pointer)
- The get pointer indicates the byte number in the
file from which the next input (for reading) is
to occur - The put pointer indicates the byte number in the
file at which the next output should be placed Â
18Some related functions..
- For istream
- tellg()
- Get position of the get pointer
- seekg()
- Set position of the get pointer
- For ostream
- tellp()
- Get position of the put pointer
- seekp()
- Set position of the put pointer
19seekg()
- istream seekg( streampos pos )
- istream seekg( streamoff off, iosseek_dir dir
) - Parameters
- pos
- The new position value streampos is a typedef
equivalent to long. - off
- The new offset value streamoff is a typedef
equivalent to long. - dir
- The seek direction. Must be one of the following
enumerators - iosbeg Seek from the beginning of the stream.
- ioscur Seek from the current position in the
stream. - iosend Seek from the end of the stream.
- Reference http//msdn.microsoft.com/library/defau
lt.asp
20tellg()
- streampos tellg()
- Function
- Gets the value for the streams get pointer.
- Return Value
- A streampos type, corresponding to a long
- Reference http//msdn.microsoft.com/library/defau
lt.asp
21seekp()
- ostream seekp( streampos pos )
- ostream seekp( streamoff off, iosseek_dir dir
) - Parameters
- pos
- The new position value streampos is a typedef
equivalent to long. - off
- The new offset value streamoff is a typedef
equivalent to long. - dir
- The seek direction. Must be one of the following
enumerators - iosbeg Seek from the beginning of the stream.
- ioscur Seek from the current position in the
stream. - iosend Seek from the end of the stream.
- Reference http//msdn.microsoft.com/library/defau
lt.asp
22tellp()
- streampos tellp()
- Remarks
- Gets the position value for the stream.
- Return Value
- A streampos type that corresponds to a long.
- Reference http//msdn.microsoft.com/library/defau
lt.asp
23Examples
- ofstream fout(orders.dat)
- ifstream fin(comparts.dat)
- fout.seekp(100,iosbeg)
- fin.seekg(-countsizeof(compart),iosend)
- Take note
- Line 3 will write at the location 100 bytes
away from the beginning of the file - Line 4 moves FPI for reading countsizeof(compar
t) bytes towards the beginning of the file,
starting from the end of the file - Offset value can be positive or negative,
indicating the direction away from or toward the
beginning of the file - After the FPI is positioned at the desired place,
reading and writing will take place
24Example
- include ltiostreamgt
- include ltfstreamgt
- using namespace std
- int main ()
-
- long begin,end
- ifstream myfile ("example.txt")
- begin myfile.tellg()
- myfile.seekg (0, iosend)
- end myfile.tellg()
- myfile.close()
- cout ltlt "size is " ltlt (end-begin) ltlt "
bytes.\n" - return 0
-
25Creating a Random-Access File
- Usually write entire struct or object to file
- This will determine the fixed-length chunk of
bytes youre writing to/reading from the file
26Functions involved in random access file
27Example
- Problem statement
- Struct compart containing part information and
its quantity - Variable count will update how many compart
structures have been created - This information will be used for writing to and
reading from the file
compart1
compart2
compart3
.
28Example
- include ltiostreamgt
- include ltiomanipgt
- include ltfstreamgt
- include ltcstdlibgt
- using namespace std
- struct compart
- char part10
- int qty
- int main()
-
- int count0
- compart newpart
- ofstream fcout("comparts.dat",iosoutiosbin
ary) - if(!fcout)
- cerrltlt"Error in file opening for writing!"
- exit(1)
- coutltlt"Enter part and quantity or Ctrl-Z to
stopgt"ltltendl - while(cingtgtnewpart.partgtgtnewpart.qty)
-
29- ifstream fcin("comparts.dat",iosiniosbinary)
- if(!fcin)
-
- cerrltlt"Error in file opening for reading!"
- exit(1)
-
- if(countgt3)
-
- coutltlt"\nLast 3 records written to the file
gt"ltltendl - coutltlt"\nPart Quantity"ltltsetiosflags(ios
left)ltltendl - fcin.seekg((count-3)sizeof(compart),iosbe
g) - int c0
- while(clt3 !fcin.eof())
-
- fcin.read((char)newpart,
sizeof(compart)) - c
- if(clt3)
- coutltltsetw(10)ltltnewpart.partltltsetw(5)ltlt
newpart.qtyltltendl -
30Review on Example
- Take note of the binary mode chosen when creating
the file object for both read and write operation - Line 20
- FPI for writing is positioned by seekp() at the
location countsizeof(compart)bytes away from the
beginning of the file - Line 21
- Writes a data record stored in newpart to the
file - By referring to the format of write(), (char)
type cast need to be applied to the address of
newpart before passing it to write() - Line 36
- FPI for reading is positioned by seekg() at the
location count-3sizeof(compart)bytes away from
the beginning of the file - Line 40
- Reads a data record stored in file to newpart
31write()
- ostream write( const char pch, int nCount )
- ostream write( const unsigned char puch, int
nCount ) - ostream write( const signed char psch, int
nCount ) - Parameters
- pch, puch, psch
- A pointer to a character array.
- nCount
- The number of characters to be written.
- Remarks
- Inserts a specified number of bytes from a buffer
into the stream. If the underlying file was
opened in text mode, additional carriage return
characters may be inserted. The write function is
useful for binary stream output. - Reference http//msdn.microsoft.com/library/defau
lt.asp
32read()
- istream read( char pch, int nCount )
- istream read( unsigned char puch, int nCount )
- istream read( signed char psch, int nCount )
- Parameters
- pch, puch, psch
- A pointer to a character array.
- nCount
- The maximum number of characters to read.
- Remarks
- Extracts bytes from the stream until the limit
nCount is reached or until the end of file is
reached. The read function is useful for binary
stream input. - Reference http//msdn.microsoft.com/library/defau
lt.asp
33Text File VS Binary File
34(No Transcript)
35Goodness of Random Access File
- Provides rapid access
- Beneficial to some applications such as
- Banking system
- Airline-reservation systems
- ATM
- Can perform further manipulations of the records
efficiently - How to retrieve the records will depend on your
design - Previous example writes in consecutive places
- Example in deitel and deitel, they are using
account number as indexing - More examples refer to deitel and deitel
slides (in my website) and the given handout
36MORE istream Member Functions peek, putback and
ignore
- ignore()
- Discards characters from stream (default 1)
- Stops discarding once delimiter found
- Default delimiter \n
- putback()
- Puts character obtained by get() back on stream
- peek()
- Returns next character in stream, but does not
remove
371 // Adapted from Deitels and Deitels text
book 2 // Writing to a random access file. 3
include ltiostreamgt 4 5 using
stdcerr 6 using stdendl 7 using
stdcout 8 using stdcin 9 using
stdios 10 11 include ltiomanipgt 12
13 using stdsetw 14 15 include
ltfstreamgt 16 17 using stdofstream 18
19 include ltcstdlibgt 20 include
"clientData.h" // ClientData class definition 21
3822 int main() 23 24 int
accountNumber 25 char lastName 15 26
char firstName 10 27 double
balance 28 29 ofstream outCredit(
"credit.dat", iosbinary ) 30 31 //
exit program if ofstream cannot open file 32
if ( !outCredit ) 33 cerr ltlt "File
could not be opened." ltlt endl 34 exit(
1 ) 35 36 // end if 37 38
cout ltlt "Enter account number " 39 ltlt
"(1 to 100, 0 to end input)\n? " 40 41
// require user to specify account number 42
ClientData client 43 cin gtgt
accountNumber 44 client.setAccountNumber(
accountNumber ) 45
3946 // user enters information, which is
copied into file 47 while (
client.getAccountNumber() gt 0 48
client.getAccountNumber() lt 100 ) 49 50
// user enters last name, first name and
balance 51 cout ltlt "Enter lastname,
firstname, balance\n? " 52 cin gtgt setw(
15 ) gtgt lastName 53 cin gtgt setw( 10 )
gtgt firstName 54 cin gtgt balance 55
56 // set record lastName, firstName
and balance values 57 client.setLastName(
lastName ) 58 client.setFirstName(
firstName ) 59 client.setBalance(
balance ) 60 61 // seek position in
file of user-specified record 62
outCredit.seekp( ( client.getAccountNumber() - 1
) 63 sizeof( ClientData ) )
64 65 // write
user-specified information in file 66
outCredit.write(
67 reinterpret_castlt const char gt(
client ), 68 sizeof( ClientData ) )
69
4070 // enable user to specify another
account number 71 cout ltlt "Enter account
number\n? " 72 cin gtgt accountNumber 73
client.setAccountNumber( accountNumber
) 74 75 // end while 76 77
return 0 78 79 // end main
41Enter account number (1 to 100, 0 to end input) ?
37 Enter lastname, firstname, balance ? Barker
Doug 0.00 Enter account number ? 29 Enter
lastname, firstname, balance ? Brown Nancy
-24.54 Enter account number ? 96 Enter lastname,
firstname, balance ? Stone Sam 34.98 Enter
account number ? 88 Enter lastname, firstname,
balance ? Smith Dave 258.34 Enter account
number ? 33 Enter lastname, firstname, balance ?
Dunn Stacey 314.33 Enter account number ? 0