140 likes | 284 Views
Binary File Input/Output. CS 1410 Version 1.0. Overview. Files are either “Text” or “Binary” Text Files – When written all data is converted to characters and then written to the file. Binary Files – When written there is a byte by byte copy directly from memory to the file.
E N D
Binary File Input/Output CS 1410 Version 1.0
Overview • Files are either “Text” or “Binary” • Text Files – When written all data is converted to characters and then written to the file. • Binary Files – When written there is a byte by byte copy directly from memory to the file. • 1st Create a file object (fstream (in/out), ifstream (in), ofstream (out)) • Mode – ios::out | ios::in | ios::binary | ios::trunc | ios::app | ios::ate
File Modes • fstream* filePtr = new fstream(); • filePtr->open(Filename,Mode); • Parameters • Filename - C-string containing the name of the file to be opened. • Mode - Flags describing the requested i/o mode for the file. This is an object of typeios_base::openmode, which consists on a combination of one or more of the following flags defined as member constants:
File Open Disk File Output buffer Input buffer fstream* filePtr = new fstream(); filePtr->open(“Test.bin”,ios::binary|ios::out|ios::in|ios::trunc); 0 1 2 3 4 5 6 7 … ……………………………………………………………………… File pointer
: Error Flags Word intestatus = filePtr->rdstate(); 0 1 2 3 4 … 31 fail bad eof
Writing & Reading Binary Files Write Binary Data filePtr->write((char*)&data,sizeof data); Seek Get Pointer to first byte in file filePtr->seekg(0,ios::beg); //ios::end, ios::cur Read Binary Data filePtr->read((char*)&data,sizeof data);
Writing a binary file Writing a binary file Data Object 0000 0101 0001 1111 filePtr->write((char*)&data,sizeof Data); 0010 0001 0011 0010 0100 0100 1000 1000 0 1 2 3 4 5 6 7 0011 0010 0100 0100 ? 0000 0101 0001 1111 0010 0001 1000 1000 ? File put pointer
Write Complete Data Object 0000 0101 0001 1111 filePtr->write((char*)write,sizeof Data); 0010 0001 0011 0010 0100 0100 1000 1000 0 1 2 3 4 5 6 7 1000 1000 eof 0000 0101 0001 1111 0010 0001 0011 0010 0100 0100 ? File put pointer
Seek Complete Data Object 0000 0101 0001 1111 filePtr->seekp(0,ios::beg); filePtr->seekg(0,ios::beg); 0010 0001 0011 0010 0100 0100 1000 1000 0 1 2 3 4 5 6 7 1000 1000 eof 0000 0101 0001 1111 0010 0001 0011 0010 0100 0100 ? File put/get pointers
Seeking the File Pointer • Parameters • pos - The new position in the stream buffer. This parameter is an integral value of type streampos. • off - Integral value of type streamoff representing the offset to be applied relative to an absolute position specified in the dir parameter. • dir - Seeking direction. It is an object of type ios_base::seekdir that specifies an absolute position from where the offset parameter off is applied. It can take any of the following member constant values: filePtr->seekg((+/-)<value>,<dir>); filePtr->seekg(-10,ios::end); //seek 10 bytes past the eof fipPtr->seekg(0,ios::beg); //seek to beginning of the file
Read Started Data Object 0000 0101 dataPtr->clear(); filePtr->read((char*)dataPtr,sizeof Data); 0 1 2 3 4 5 6 7 1000 1000 eof 0000 0101 0001 1111 0010 0001 0011 0010 0100 0100 ? File put/get pointers
Read Completed Data Object 0000 0101 0001 1111 filePtr->read((char*)&Data,sizeof Data); 0010 0001 0011 0010 0100 0100 1000 1000 0 1 2 3 4 5 6 7 1000 1000 eof 0000 0101 0001 1111 0010 0001 0011 0010 0100 0100 ? File get pointer
Important fstream File Functions Member Functions ios Member Functions • rdbuf() • open() • close() • read() • tellg() • seekg() • write() • tellp() • seekp() • flush() • good() • eof() • bad() • rdstate() • setstate() • clear() • exceptions() • flags() • setf() • unsetf()