csc1254lect02

csc1254lect02 - CSc 1254: Lecture # 2 Binary Files August...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSc 1254: Lecture # 2 Binary Files August 24, 2005 Binary Files Binary File I/O Commands Sample Program 1 Binary Files A binary file is one in which data is written in bytes used for storage, rather than in ASCII format. You may think of this as copying a segment of memory. Binary files are very similar to arrays of structures, except the structures are in a disk file rather than in an array in memory. Because the structures in a binary file are on disk, you can create very large collections of them. They are also permanent and always available. Binary files also usually have faster read and write times than text files, because a binary image of the record is stored directly from memory to disk (or vice versa). ASCII files must have everything converted back and forth. 1.1 Advantages 1. Less space is used as compared to ASCII, sequential, files. 2. Aggregate data structures, for example arrays and structs, may be written at once to a binary file. 3. Each item is usually of the same size. 4. Data item in a binary file may be accessed randomly. 1 1.2 Disadvantages 1. A binary file cannot be directly edited, printed or read. 2. A binary file is architecture-dependent. There may be problems involv- ing byte misalignment when switching platforms. 2 Binary File I/O Commands Figure 1: File I/O 2 2.1 Opening A Binary File A file stream object can be opened two ways: blue ifstream fsobj ("data.bin", ios::in | ios::out | ios::binary); ofstream fsobj; ... fsobj.open ("data2.bin", ios::in | ios::out | ios::binary); The second parameter (the i/o mode parameter) can be omitted when dealing with text files. However, the i/o parameter must be included when dealing with binary files: that is, always include ios::binary as one of the mode flags. For read/write access to a file, use an fstream. For read accessmode flags....
View Full Document

Page1 / 9

csc1254lect02 - CSc 1254: Lecture # 2 Binary Files August...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online