1 / 13

Binary File Input/Output

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.

lam
Download Presentation

Binary File Input/Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary File Input/Output CS 1410 Version 1.0

  2. 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

  3. 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:

  4. 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

  5. : Error Flags Word intestatus = filePtr->rdstate(); 0 1 2 3 4 … 31 fail bad eof

  6. 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);

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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()

More Related