fread() and fwrite() functions in C - C-Tutorial

Latest

Thursday, 10 November 2016

fread() and fwrite() functions in C

fread() and fwrite() functions:

fread() and fwrite() functions are used to read and write data in binary format.  The general formats of fread() and fwrite() functions are:

            Syntax:            fread(&x, sizeof(x), 1, FilePointer);
                                    fwrite(&x, sizeof(x), 1, FilePointer);

Here,
            The first argument is address of the argument.
            The second argument is size of the argument in bytes.
            The third argument is number of arguments read or write at one time.
            The final argument is the FilePointer.


/* WRITE A PROGRAM TO DEMONSTRATE THE DIFFERENCE BETWEEN TEXT MODE Vs BINARY MODE FORMATS */

#include<stdio.h>
main()
{
            int x;
            float y;
            char z='\n';
            FILE *fp;
            clrscr();
            fp=fopen("Exam.txt","w");
            printf("\nEnter One Integer and One Floating Number:\n");
            scanf("%d%f",&x,&y);
            fprintf(fp,"%d\n%f",x,y);
            fclose(fp);
            fp=fopen("Del.txt","wb");
            fwrite(&x,sizeof(x),1,fp);
            fwrite(&z,sizeof(z),1,fp);
            fwrite(&y,sizeof(y),1,fp);
            fclose(fp);

}

No comments:

Post a Comment