Table of Contents
This guide covers the function fread ()() in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
Declare the Function Fread ()() in C
Below is the declaration for the fread () function in C:
size_t fread ( void * ptr , size_t size , size_t nmemb , FILE * stream )
Parameters
Ptr - This is a pointer to a memory block with a minimum size of size * nmemb bytes.
Size - This is the size (byte value) of each element to be read.
Nmemb - This is the number of elements, with each element having the size of byte size.
Stream - This is the pointer to a FILE object that defines an Input Stream.
Returns the value
The total number of successfully read elements is returned as a size_t object, which is an integer data type. If this number is different from the nmemb parameter, then an error has occurred or End-Of-File has been encountered.
For example
The following program C illustrates the usage of the fread () function in C:
#include #include int main () { FILE * fp ; char c [] = "Hoc C co ban va nang cao tai QTM !!!" ; char buffer [ 100 ]; /* mo file de doc va ghi */ fp = fopen ( "baitapc.txt" , "w+" ); /* Ghi du lieu vao file */ fwrite ( c , strlen ( c ) + 1 , 1 , fp ); /* thiet lap vi tri con tro tim kiem ve dau file */ fseek ( fp , SEEK_SET , 0 ); /* Doc va hien thi du lieu */ fread ( buffer , strlen ( c )+ 1 , 1 , fp ); printf ( "%sn" , buffer ); fclose ( fp ); return ( 0 ); }
Compile and run the above C program to see the results:
According to Tutorialspoint
Previous lesson: Function fopen () in C
Next lesson: Function freopen () in C
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.