Working with Memory Blocks

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

Info icon This 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 *src,
Image of page 1

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

View Full Document Right 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
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") rather than:
Image of page 3

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

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

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

View Full Document Right Arrow Icon
Image of page 6
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern