Showing posts with label fread in c. Show all posts
Showing posts with label fread in c. Show all posts

Thursday, December 17, 2020

, ,

Example program for fread function in c

 fread function in c used to read the data from desired file and stored it into a buffer.

syntax For fread c: -

size_t fread(void * buffer, size_t size, size_t count, FILE * stream)

Parameters For fread in c 

fread c use 4 parameters in it's program and these parameters are follow  :- 

Buffer : -  buffer is also know as the space which is used to temporarily stored the data.

it did the same work for fread function in c. 

 Size: This one is an another parameters which is used to read the size in byte for each elements.

Count: Used to count the number of elements (like file for fread in c).

Stream :- Pointer is used for file object to tell the place where data is going to be read.

Return value For fread function in c: -

It return the integer value at the time of success which is equal to the count otherwise error if  value less than count.

Example for the fread c

#include <stdio.h>

int main() {
  char buffer[20]; // Buffer to store data
  FILE * stream;
  stream = fopen("file.txt", "r");
  int count = fread(&buffer, sizeof(char), 20, stream);
  fclose(stream);
  // Printing data to check validity
  printf("Data read from file: %s \n", buffer);
  printf("Elements read: %d", count);
  return 0;
}
OUTPUT

fread c
fread c