LIVE Course for free

Rated by 1 million+ students
Get app now
0 votes
in Computer by (45.8k points)
closed by

Explain fread () and fwrite () functions.

1 Answer

+1 vote
by (45.4k points)
selected by
Best answer

fread( ): This function reads a specified number of equalsized data items from an input stream into a block, fread returns the number of items (not bytes) actually read on success.


fread (& name_of_structure, size_of_(structure name), l, file pointer); e.g.,

struct employee
char nm[20]; /* 20 bytes */
int age; /* 2 bytes */
struct employee Emp;
fread (&Emp, size of (Emp), 1, fp);

Here, the fread function can read 22 bytes of information from the file pointed by file pointer fp. fwrite( ): This function appends a specified number of equal-sized data items to an output file.


fwrite (& struct—name, size of (struct), 1, fp); e.g.,

street address
char city [30]; /* 30 bytes */
long in pin; /* 2 bytes */
char country [20]; /* 20 bytes */
struct address add;
FILE *fp;
fwrite (& add, size of (add), 1, fp);

Related questions

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.