Q:

C program to read a variable from a file using the fread

belongs to collection: File handling in C

0

Reading a variable from a file using the fread

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to read an array from the file using the... >>
<< How to use fread in c...