Working with Memory Blocks

Working with Memory Blocks - • position within the file....

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

View Full Document Right Arrow Icon
Working with Memory Blocks Operations on memory blocks pointed to by generic pointers are declared in the standard header file string.h . Generic pointers can not be dereferenced. To copy a memory block of size len from a source src to a destination dest (these blocks shouldn't overlap): void *memcpy(void *dest, const void
Background image of page 1

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

View Full DocumentRight Arrow Icon
8: File I/O for Memory Blocks: Binary Files Binary files do not have a line-oriented structure They consist of blocks of objects , (for example, double objects) which allows them to store information in a concise way. Many applications can process binary data efficiently, but to display this data in a human-readable form, a specialized
Background image of page 2
Binary Files When you open binary files, you should always use b in the second argument for fopen() For example, use: fopen("test.bin", "wb")
Background image of page 3

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

View Full DocumentRight Arrow Icon
8: Binary Files Random Access Random access : you can directly operate on data stored at any
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: • position within the file. • Functions that can be used to operate on binary files • (assuming that the file has been opened): 8: Binary Files Random Access • int fseek(FILE *f, long offset, int mode) • sets the current position in a file by the specified offset • returns 0 if successful and a non-zero value otherwise. • There are three predefined macros: •- SEEK_SET the offset is from the 8: Binary Files Random Access • Examples, for a file of double values: • fseek(f, sizeof(double), SEEK_SET) • fseek(f, sizeof(double), SEEK_CUR) • fseek(f, -sizeof(double), SEEK_CUR) • fseek(f, -sizeof(double), SEEK_END) Example 8-3 • /* compute the size of a file */ • long fileSize(const char *filename) { • FILE *f; • long size; • if((f = fopen(filename, "r")) == NULL) • return -1L; • if(fseek(f, 0L, SEEK_END) == 0) { • size = ftell(f);...
View Full Document

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue.

Page1 / 7

Working with Memory Blocks - • position within the file....

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

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