1402 18 - 3/14/2010 COMP1002/1402...

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

View Full Document Right Arrow Icon
3/14/2010 1 COMP 1002/1402 Introduction to Systems Programming Chapter 18: Binary Files Notes Adapted from Dr. J. Morrison, Dr D. Nussbaum File Types C has two main types: Binary Files Everything stored as 0’s and 1’s Text Files Usually human readable characters Each data line ends with newline char 2 COMP1002/1402 - Chapter 18: Binary Files
Background image of page 1

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

View Full DocumentRight Arrow Icon
3/14/2010 2 Binary files A permanent storage of data which is kept in the format of the hardware format of the hardware. A “mirror” image of the memory of the computer. Purpose Provide a copy of the memory for later usage Transferring data from one program to another Recovery in cases where computation has failed (checkpoints). COMP1002/1402 - Chapter 18: Binary Files 3 Opening Binary Files Similar to text files FILE *fopen(char *filename char *mode) FILE fopen(char filename, char mode); Mode Meaning rb Open file for reading If file exists, the marker is positioned at beginning b Open file for writing If file exists it is truncated Beware !! COMP1002/1402 - Chapter 18: Binary Files 4 wb If file exists, it is truncated. Beware !!! If file doesn’t exist, it is created. ab Open text file for append •If file exists, the marker is positioned at end. •If file doesn’t exist, it is created.
Background image of page 2
3/14/2010 3 Opening Binary Files (2) Using the “+” character means opened for read and write Mode Meaning r+b Open file for reading If file exists, the marker is positioned at beginning +b Open file for writing If file exists it is truncated Beware !! COMP1002/1402 - Chapter 18: Binary Files 5 w+b If file exists, it is truncated. Beware !!! If file doesn’t exist, it is created. a+b Open text file for append •If file exists, the marker is positioned at end. •If file doesn’t exist, it is created. fopen(fileName, mode) rb, r+b file is positioned at the beginning wb, w+b file is positioned at the beginning ab, a+b file is positioned at the end COMP1002/1402 - Chapter 18: Binary Files 6
Background image of page 3

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

View Full DocumentRight Arrow Icon
3/14/2010 4 Binary File Organization File is organized like the memory as a byte stream. First byte in the file is the at position 0. File is termed binary file because it stores the binary representation of numbers . Eg 255 will be stored in a single byte as 0xF E.g., 255 will be stored in a single byte as 0xFF In text file it will be stored in three bytes ‘2’’5’’5’ (0x32 0x35 0x35) COMP1002/1402 - Chapter 18: Binary Files 7 Binary File Organization (2) Information in the file mimics the internal memory. Therefore, data can be copied to an from the file without translation/conversion. Data in the file is meaningful only to the program that reads or writes to the file .
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/11/2010 for the course COMP 1402 taught by Professor Sprite during the Spring '10 term at Carleton CA.

Page1 / 21

1402 18 - 3/14/2010 COMP1002/1402...

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

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