Binary Files CS362

advertisement
Binary Files
CS362
Binary Files



Binary File hold data in binary format.
Data in file is actual bit representation for actual data
This is more efficient:

No data conversion required






No data conversion to ASCII when writing data to the file
No data conversion to bit representation when reading data from file
No end-of-line characters stored in data.
A binary file is a simply a stream of data.
More efficient but less portable across different machine
architectures.
Files are specific to the application.
Binary Files

Cannot be created in a text editor



A program must create them
File contains a sequence of bytes that are accessible via an
offset from the top of the file
Originating program specifies what the bytes represent
Binary Files

Every file has two “position pointers”




The current reading position (get pointer)
The current writing position (put pointer)
Binary files can be one of three file streams
Each file stream has at least one of the “position pointers”
ifstream file streams have a get pointer
 ofstream file streams have a put pointer
 fstream file streams have both a get and put pointer
The “position pointers” are independent – can be moved to any point
in the file




During a file read the get pointer is moved automatically to the next unit of data
During a file write the put pointer is moved automatically to the next unit of data
Binary Files




Still requires a file stream to be declared
Still requires the stream to be opened (open())
Difference is we need to specify the mode:
Modes that can be use are:






ios::in
ios::out
ios::ate
ios::app
ios::trunc
ios::binary
Open file for reading
Open file for writing
Open file with initial position at end of file
Every output is appended at the end of file
If the file previously existed, it is erased
Binary mode
Binary Files


Mode flags can be combined using the bitwise operator OR
(‘|’) (different than logic OR (‘||’))
For example:
ofstream outFile;
outFile.open(“data.bin”, ios::out | ios:binary);

There are default modes:
ofstream – output file stream (defaults – ios::out | ios::trunc)
ifstream – input file stream (defaults – ios::in)
fstream – input/output file stream (defaults – ios::in | ios::out)

Binary Files



Default values are only applied if open function is called
without specifying any mode parameters
Any modes specified in the open file process, overwrites all
defaults
Specifying a file as binary means you must include the
mode in the open process, therefore you must also specify
the in/out modes
Binary Files


Once file is open the “read” and “write” functions are used
to access the file
Data read from the file using the “read” function:


Data written to the file using the “write” function:



filestreamName.read (char* dataPointer, int numBytes);
filestreamName.write(char* dataPointer, int numBytes);
Data is read or written one byte at a time
The most common use is to write or read one record at a
time
Download