Chapter 12 Advanced File Operations123
Chapter 12 Advanced File Operations123
Operations
Writing a file with an ofstream object Reading a file with an ifstream object
File Operations (continued)
fixed
setw(n)
Passing File Stream Objects to Functions
Function Prototypes declare the functions.
Note that the file object must be passed to the
function by reference. (This uses a reference
variable - not a pointer variable)
Copies the data from the source file to the Data destination file which doesnt have
destination file. to exist at the beginning of the program
Binary Files
Use the write() and read() member functions:
fileObject.write(address, size) - address it the starting
address of the memory to be written to the file, size is the
number of bytes of memory to write
fileObject.read(address, size) - address is the starting
address of memory where the data read from the file is to be
stores, size is the number of bytes of memory to read from the
file
The address is assumed to be a pointer to a character so if
the data is not a character, it must be re-cast using the
reinterpret_cast type cast. (e.g. the data is a struct, the struct
pointer must be reinterpret_cast as a char pointer)
The program on the next slide uses the .write() and .read()
member functions.
Creating Records with Structures
Records can be created by writing structures to a file