Q:

How to use fread in c

belongs to collection: File handling in C

0

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.

All Answers

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

#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:

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

total answers (1)

C program to read a variable from a file using the... >>
<< create a file using fopen in C...