fread() - I/O Function

How is "fread()" used in C++?

Explanation

fread() is an I/O function which is used to read the
specified number of objects, of the "size" from a specified stream and places them in an array. This function returns the
actual number of items, if less numbers are shown then there might be an error or EOF has reached.

Syntax:



size_t fread( void *buf, size_t size,
size_t count, FILE * stream);


Example :





#include <stdio.h>
int main()
{
FILE *ptr;
char str[30];
char *d;
int i;
ptr = fopen("freadeg.txt", "r");
i = fread(str, 1,5,ptr);
str[i] = '';
printf("The characters read are::%s\n", str);
printf("Number of characters read:: %d\n\n", i);
fclose(ptr);
}


Result :

The characters read are::01234
Number of characters read:: 5



In the above example the fread() is used to read first "5" character from a text file.

C++ Tutorial


Ask Questions

Ask Question