Previous | Home | Next |
Description
The C library function, The fread() function reads into the array pointed to by ptr up to nitems elements whose size is specified by size in bytes, from the stream pointed to by stream.
Declaration
Following is the declaration for fread() function.
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Parameters
ptr - The array where the elements will be stored.
size - The size of each element in bytes.
nitems - The number of elements to read.
stream - The stream to read.
Return Value
The fread( ) function returns the number of items read. This value may be less than nmemb if the end of the file is reached or an error occurs.
Example
#include <stdio.h> #include <string.h> int main() { FILE *fp; char c[] = "this is tutorial"; char buffer[100]; /* Open file for both reading and writing */ fp = fopen("file.txt", "w+"); /* Write data to the file */ fwrite(c, strlen(c) + 1, 1, fp); /* Seek to the beginning of the file */ fseek(fp, SEEK_SET, 0); /* Read and display data */ fread(buffer, strlen(c)+1, 1, fp); printf("%s\n", buffer); fclose(fp); return(0); }
Output
this is tutorial
Previous | Home | Next |