Read binary data from a stream.
Compatibility:This function is compatible with the following targets:
|
|
#include <stdio.h>
size_t fread(void *ptr, size_t size,  
size_t nmemb, FILE *stream);
Parameters for this facility are:
The fread() function reads nmemb items unless it reaches the end-of-file or a read error occurs.
NOTE
fread() returns the number of items read successfully.
"Wide Character and Byte Character Stream Orientation"
Example of fread() usage.:#include <stdio.h>
#include <stdlib.h>
// define the item size in bytes
#define BUFSIZE 40
int main(void)
{
FILE *f;
static char s[BUFSIZE] = "The quick brown fox";
char target[BUFSIZE];
// create a new file for output and input
if (( f = fopen("foo", "w+")) == NULL) {
printf("Can't create file.\n");
exit(1);
}
// output to the stream using fwrite()
fwrite(s, sizeof(char), BUFSIZE, f);
// move to the beginning of the file
rewind(f);
// now read from the stream using fread()
fread(target, sizeof(char), BUFSIZE, f);
// output the results to the console
puts(s);
puts(target);
// close the file
fclose(f);
return 0;
}
Output:
The quick brown fox
The quick brown fox
[ First ] [ Previous ] [ Next ] [ Last ] [ Manuals ]
Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.
Last updated: August 16, 2000