1 / 4

fread and fwrite functions are the most efficient way to read or write large amounts of data.

Block I/O. fread and fwrite functions are the most efficient way to read or write large amounts of data. fread () – reads a specified number of bytes from a binary file and places them into memory at the specified location . prototype for fread ( ):

Download Presentation

fread and fwrite functions are the most efficient way to read or write large amounts of data.

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. Block I/O • fread and fwrite functions are the most efficient way to read or write large amounts of data. • fread() – reads a specified number of bytes from a binary file and places them into memory at the specified location. • prototype for fread( ): • intfread(void *InArea, intelementSize, int count, FILE *fp); • InArea – a pointer to the input area in memory. • elementSize– size of a basic data element, often specified using the sizeof operator • count – number of elements • fp – file pointer of an open file. • fread returns the number of items read.

  2. Block I/O • fwrite – writes the specified bytes of data from memory to the output file. • prototype for fwrite ( ): • intfwrite(void *OutArea, intelementSize, int count, FILE *fp); • OutArea – a pointer to memory holding the data to be written. • elementSize – size of a basic data element, often specified using the sizeofoperator • count – number of data elements • fp – file pointer of an open file. • fwrite copies elementSize * count bytes from the address specified by OutArea to the file. • fwrite returns the total number of characters written.

  3. Block input and output • Here is a more efficient implementation of the cat-like program that copies standard input to the standard output. • /* p12.c */ • #include <stdio.h> • main() • { • unsigned char *buff; • intlen = 0; • intiter = 0; • buff = malloc(1024); • if (buff == 0) • exit(1); • while ((len = fread(buff, 1, 1024, stdin)) != 0) • { • fwrite(buff, 1, len, stdout); • fprintf(stderr, "%d %d \n", iter, len); • iter += 1; • } • } • 0 322

  4. Block input and output Questions: Removing the parentheses surrounding (len = fread(buff, 1, 1024, stdin)) will break the program. Explain exactly how and whythings will go wrong in this case? What will happen if lenin the fwrite() is replaced by 1024?

More Related