Read Integers Into Array Until End of File

fread() Function in C

Last updated on July 27, 2020


The fread() role is the complementary of fwrite() function. fread() part is ordinarily used to read binary data. It accepts the same arguments as fwrite() part does. The syntax of fread() function is as follows:

Syntax: size_t fread(void *ptr, size_t size, size_t due north, FILE *fp);

The ptr is the starting accost of the retention block where data will be stored afterwards reading from the file. The function reads north items from the file where each item occupies the number of bytes specified in the 2d argument. On success, information technology reads n items from the file and returns north. On error or end of the file, it returns a number less than northward.

Let's have some examples:

Example i: Reading a float value from the file

                                            int                      val                      ;                      fread                      (                      &                      val                      ,                      sizeof                      (                      int                      ),                      1                      ,                      fp                      );                    

This reads a bladder value from the file and stores information technology in the variable val.

Instance two: Reading an array from the file

                                            int                      arr                      [                      ten                      ];                      fread                      (                      arr                      ,                      sizeof                      (                      arr                      ),                      one                      ,                      fp                      );                    

This reads an assortment of 10 integers from the file and stores information technology in the variable arr.

Instance 3: Reading the starting time 5 elements of an array

                                            int                      arr                      [                      x                      ];                      fread                      (                      arr                      ,                      sizeof                      (                      int                      ),                      five                      ,                      fp                      );                    

This reads v integers from the file and stores it in the variable arr.

Case four: Reading the starting time 5 elements of an array

                                            int                      arr                      [                      x                      ];                      fread                      (                      arr                      ,                      sizeof                      (                      int                      ),                      5                      ,                      fp                      );                    

This reads 5 integers from the file and stores information technology in the variable arr.

Instance five: Reading the structure variable

                                            struct                      student                      {                      char                      name                      [                      10                      ];                      int                      gyre                      ;                      float                      marks                      ;                      };                      struct                      student                      student_1                      ;                      fread                      (                      &                      student_1                      ,                      sizeof                      (                      student_1                      ),                      ane                      ,                      fp                      );                    

This reads the contents of a structure variable from the file and stores it in the variable student_1.

Case 6: Reading an array of structure

                                            struct                      educatee                      {                      char                      proper noun                      [                      10                      ];                      int                      roll                      ;                      float                      marks                      ;                      };                      struct                      student                      arr_student                      [                      100                      ];                      fread                      (                      &                      arr_student                      ,                      sizeof                      (                      struct                      student                      ),                      10                      ,                      fp                      );                    

This reads first x elements of type struct educatee from the file and stores them in the variable arr_student.

The post-obit program demonstrates how we tin apply fread() function.

                      one  2  3  four  5  6  7  8  ix x eleven 12 xiii 14 xv sixteen 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
                                            #include                      <stdio.h>                                            #include                      <stdlib.h>                                            struct                      employee                      {                      char                      name                      [                      l                      ];                      char                      designation                      [                      50                      ];                      int                      age                      ;                      float                      salary                      }                      emp                      ;                      int                      principal                      ()                      {                      FILE                      *                      fp                      ;                      fp                      =                      fopen                      (                      "employee.txt"                      ,                      "rb"                      );                      if                      (                      fp                      ==                      Zippo                      )                      {                      printf                      (                      "Error opening file                      \n                      "                      );                      get out                      (                      1                      );                      }                      printf                      (                      "Testing fread() function:                                            \north\n                      "                      );                      while                      (                      fread                      (                      &                      emp                      ,                      sizeof                      (                      emp                      ),                      1                      ,                      fp                      )                      ==                      1                      )                      {                      printf                      (                      "Name: %s                                            \n                      "                      ,                      emp                      .                      name                      );                      printf                      (                      "Designation: %s                                            \n                      "                      ,                      emp                      .                      designation                      );                      printf                      (                      "Age: %d                                            \due north                      "                      ,                      emp                      .                      age                      );                      printf                      (                      "Salary: %.2f                                            \n\n                      "                      ,                      emp                      .                      bacon                      );                      }                      fclose                      (                      fp                      );                      return                      0                      ;                      }                    

Expected Output:

                      Testing fread() function:  Name: Bob Designation: Manager Age: 29 Salary: 34000.00  Proper name: Jake Designation: Developer Age: 34 Salary: 56000.00                    

How it works:

In lines four-10, a structure employee is alleged along with a variable emp . The structure employee has four members namely: name, designation, age and salary.

In line xiv, a construction arrow fp of type struct FILE is declared.

In line 15, fopen() role is called with two arguments namely "employee.txt" and "rb". On success, it returns a pointer to file employee.txt and opens the file employee.txt in read-only mode. On failure, it returns Null.

In lines 17-21, if statement is used to test the value of fp. If information technology is NULL, printf() argument prints the mistake message and program terminates. Otherwise, the program continues with the statement following the if statement.

In lines 25-31, a while loop is used forth with fread() to read the contents of the file. The fread() role reads the records stored in the file one by ane and stores it in the structure variable emp. The fread() function will keep returning 1 until there are records in the file. As soon equally the end of the file is encountered fread() will return a value less than 1 and the condition in the while loop become false and the command comes out of the while loop.

In line 33, fclose() part is used to close the file.



Ezoic

fralickthowithid.blogspot.com

Source: https://overiq.com/c-programming-101/fread-function-in-c/

0 Response to "Read Integers Into Array Until End of File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel