Pages

Thursday, March 27, 2014

C Program to demonstrate use of fwrite() to write into a file

/*eg of fwrite( )*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
    char name[25];
    int rollno;
    float fee;
};
int main( )
{
    struct student s;
    FILE *ptvar;
    ptvar=fopen ("record.dat","wb");
    do
    {
        printf ("Enter Name: ");
        scanf ("%s",s.name);
        printf ("Enter rollno: ");
        scanf ("%d",&s.rollno);
        printf ("Enter fee: ");
        scanf ("%f",&s.fee);
        fwrite (&s, sizeof(s),1,ptvar);
        printf ("\nAny more records?(y/n): ");
        fflush(stdin);
    }while (getchar( )=='y');
    fclose (ptvar);
    system ("pause");
    return 0;
}

No comments:

Post a Comment