Friday 7 September 2012

creating a file of students in C

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main(void)
{
    FILE *stf;
    typedef struct st
        {
            int rno;
            char name[15];
            int subj[3];
        } stud;
    char fname[12];
    int i,j,n;
    stud student;

    clrscr();
    printf("Program for Creating a File of Students\n");
    printf("\nEnter a file name to store students details : ");
    scanf("%s",fname);
    flushall();
    stf = fopen(fname,"ab");
    if (stf == NULL)
      {
        printf("File Opening Error\n");
        exit(0);
      }
    printf("Enter how many students : ");
    scanf("%d",&n);
    for (i=1;i<=n;i++)
       {
        printf("Enter %d Student details\n",i);
        printf("Enter Student %d Roll No. : ",i);
        scanf("%d",&student.rno);
        flushall();
        printf("Enter Student %d Name : ",i);
        gets(student.name);
        printf("Enter Student %d Marks\n",i);
        for (j=1;j<=3;j++)
           {
            printf("Enter Subject %d marks : ",j);
            scanf("%d",&student.subj[j-1]);
           }
        fwrite(&student,sizeof(student),1,stf);
       }
    fclose(stf);
    printf("\nPress any key to end...");
    getch();
}

No comments:

Post a Comment