How to use fread in c, you should know

how to use fread in C language

The fread function reads nmemb elements from the given stream to the given array.
for each element, fgetc is called size times (count of bytes for a single element) and file
position indicator for the stream is advanced by the number of characters read.

It is declared in stdio.h and takes four arguments. The fread function generally used for binary file to read the binary data from the given file stream.

Syntax of fread:

//Syntax of fread

size_t fread(void * restrict ptr, size_t size, size_t nmemb,
             FILE * restrict stream);

Where,

ptr: Pointer to a block of memory with a minimum size of size*nmemb bytes.

size: Size in bytes of each element to be read.

nmemb: Number of elements to be read.

stream: Pointer to the file, from where data will be read.

You might like these articles,

 

Return value of fread():

On success, it returns the number of elements successfully read. On error, it returns a number of elements less than nmemb.

Note: If the size or nmemb is zero, fread returns zero and the contents of the array and the state of the input stream remain unchanged.

Example code of fread,

In this example, I am reading 6 characters from the file “aticleworld.txt” using the fread function.

#include <stdio.h>
 
//Maximum size of the array
#define MAX_SIZE  32
 
int main()
{
    //file pointer
    FILE *fp = NULL;
    char readFileData[MAX_SIZE] = {0};
 
    //open the file
    fp = fopen("aticleworld.txt", "r");
    if(fp == NULL)
    {
        printf("Error in opening the file\n");
        exit(1);
    }
 
    // Read 5 character from stream
    fread(readFileData,sizeof(char),6, fp);
 
    //Display read data
    puts(readFileData);
 
    //close the file
    fclose(fp);
 
    printf("Read file successfully\n");
 
    return 0;
}

Output:

fread in c

 

 

Reading a variable from a file using the fread:

Open the file in reading mode. If fopen function, open the file successfully, then using the fread function we can read the value of the variable.

#include <stdio.h>

int main()
{
    //Variable to store read value
    int data  = 0;
    //file pointer
    FILE *fp = NULL;

    //open the existing binary file
    fp = fopen("aticleworld.dat", "rb");
    if(fp == NULL)
    {
        printf("Error in opening the file\n");
        exit(1);
    }
    //read variable value from file
    fread(&data, sizeof(data), 1, fp);

    fclose(fp);

    return 0;
}

 

 

Reading an array from the file using the fread:

The below code reads 5 elements from the file and stores it in data (an integer array).

#include <stdio.h>

int main()
{
    //Reading element of array
    int data[10]  = {0};
    //file pointer
    FILE *fp = NULL;

    //open the existing binary file
    fp = fopen("aticleworld.dat", "rb");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Reads 5 element from the file and stores it in data.
    fwrite(data, sizeof(data[0]),5, fp);

    fclose(fp);

    return 0;
}

 

 

Reading a structure variable from the file using the fread in C:

The below code using the fread in C, read the contents of a structure variable from the file and stores it in the structure variable sAmlendraInfor.

#include <stdio.h>

//structure
typedef struct
{
    int id;
    char fName[16];
    char lName[16];
} s_employee;

int main()
{
    //structure variable
    s_employee sAmlendraInfor =  {0};
    //file pointer
    FILE *fp = NULL;

    //open the existing file
    fp = fopen("aticleworld.dat", "rb");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Reads the contents of a structure variable from file
    while(fread(&sAmlendraInfor, sizeof(sAmlendraInfor),1, fp) == 1)
    {
        printf("id: %d \n", sAmlendraInfor.id);
        printf("First Name: %s \n", sAmlendraInfor.fName);
        printf("Last Name: %s \n", sAmlendraInfor.lName);
    }


    fclose(fp);

    return 0;
}

Output:

read structure variable from file

 

 

Reading an array of structure using fread in C:

The below code using the fread function reads the first 5 elements of structure array from the file and stores it in the structure array sAticleworldEmplInfo.

#include <stdio.h>

//structure
typedef struct
{
    int id;
    char fName[16];
    char lName[16];
} s_employee;

int main()
{
    //Array of structure variable
    s_employee sAticleworldEmplInfo [10]=  {0};
    //file pointer
    FILE *fp = NULL;

    //open the existing file
    fp = fopen("aticleworld.dat", "rb");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Reads the five first element of the array of structure
    fread(sAticleworldEmplInfo, sizeof(sAticleworldEmplInfo),5, fp);

    fclose(fp);

    return 0;
}

 



Leave a Reply

Your email address will not be published. Required fields are marked *