C++ How to Take Input From File and Read Lines and Store Them in Struct

fread() Function in C

Last updated on July 27, 2020


The fread() function is the complementary of fwrite() function. fread() office is normally used to read binary data. It accepts the aforementioned arguments equally fwrite() office does. The syntax of fread() function is as follows:

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

The ptr is the starting accost of the memory block where data will be stored after reading from the file. The function reads northward items from the file where each particular occupies the number of bytes specified in the second statement. On success, information technology reads n items from the file and returns n. On error or terminate of the file, it returns a number less than due north.

Let's accept 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 it in the variable val.

Example two: Reading an assortment from the file

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

This reads an array of 10 integers from the file and stores it in the variable arr.

Example three: Reading the get-go 5 elements of an array

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

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

Example 4: Reading the starting time 5 elements of an array

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

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

Example five: Reading the structure variable

                                            struct                      student                      {                      char                      name                      [                      10                      ];                      int                      roll                      ;                      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.

Instance 6: Reading an assortment of structure

                                            struct                      educatee                      {                      char                      name                      [                      ten                      ];                      int                      curl                      ;                      bladder                      marks                      ;                      };                      struct                      pupil                      arr_student                      [                      100                      ];                      fread                      (                      &                      arr_student                      ,                      sizeof                      (                      struct                      student                      ),                      10                      ,                      fp                      );                    

This reads first 10 elements of blazon struct student from the file and stores them in the variable arr_student.

The following plan demonstrates how nosotros tin use fread() function.

                      one  two  3  four  5  6  seven  eight  9 10 11 12 thirteen 14 15 16 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                      [                      50                      ];                      char                      designation                      [                      l                      ];                      int                      age                      ;                      float                      salary                      }                      emp                      ;                      int                      primary                      ()                      {                      FILE                      *                      fp                      ;                      fp                      =                      fopen                      (                      "employee.txt"                      ,                      "rb"                      );                      if                      (                      fp                      ==                      NULL                      )                      {                      printf                      (                      "Error opening file                      \n                      "                      );                      exit                      (                      one                      );                      }                      printf                      (                      "Testing fread() role:                                            \n\north                      "                      );                      while                      (                      fread                      (                      &                      emp                      ,                      sizeof                      (                      emp                      ),                      1                      ,                      fp                      )                      ==                      1                      )                      {                      printf                      (                      "Name: %s                                            \n                      "                      ,                      emp                      .                      name                      );                      printf                      (                      "Designation: %south                                            \northward                      "                      ,                      emp                      .                      designation                      );                      printf                      (                      "Age: %d                                            \n                      "                      ,                      emp                      .                      age                      );                      printf                      (                      "Salary: %.2f                                            \due north\north                      "                      ,                      emp                      .                      salary                      );                      }                      fclose                      (                      fp                      );                      return                      0                      ;                      }                    

Expected Output:

                      Testing fread() role:  Name: Bob Designation: Manager Historic period: 29 Bacon: 34000.00  Name: Jake Designation: Developer Age: 34 Salary: 56000.00                    

How it works:

In lines 4-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 14, a construction pointer fp of type struct FILE is declared.

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

In lines 17-21, if argument is used to test the value of fp. If it is NULL, printf() statement prints the error message and plan terminates. Otherwise, the program continues with the statement post-obit the if statement.

In lines 25-31, a while loop is used along with fread() to read the contents of the file. The fread() role reads the records stored in the file one by 1 and stores information technology in the structure variable emp. The fread() function volition go on returning 1 until at that place are records in the file. As soon as the end of the file is encountered fread() volition return a value less than 1 and the condition in the while loop get fake and the control comes out of the while loop.

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



Ezoic

papkesannater.blogspot.com

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

0 Response to "C++ How to Take Input From File and Read Lines and Store Them in Struct"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel