Saturday, July 24, 2010

Program to get data from user and store in STUDENT file, again read it from STUDENT file and write to DATA file and display same on screen ( GTU Sem I Program No : 41)

/*Program to get student data from user,
  write it to STUDENT file , read it again in
  store in DATA file and display same on screen
  Program No   : 41 of GTU Practice Programs SEM I
  Developed by : Malhar Vora
  Developed on : 21/07/2010
  Email        : vbmade2000@gmail.com
  Web Site     : http://malhar2010.blogspot.com
**************************************************/


struct studata
{
   int rollno;
   char name[20];
};

typedef struct studata student;
student s;

//Get Student data from user
void readData()
{
    short temp;
    printf("Enter Roll No of Student :");
    scanf("%d",&temp);
    s.rollno=temp;

    fflush(stdin);
    printf("\nEnter Name of Student :");
    gets(s.name);

}

//Write inputted data to STUDENT file
void writeStudentFile()
{
   FILE *stufile;
   stufile=fopen("STUDENT","wt");
   if(stufile==NULL)
   {
      printf("\nError in writing STUDENT file");
      return;
   }
   fwrite(&s,sizeof(s),1,stufile);
   fclose(stufile);
}

//Clear structure s to hold new data
void clearData()
{
   s.rollno=0;
   strcpy(s.name,"");
}

//Read data from STUDENT file
void readStudentFile()
{
   FILE *stufile;
   stufile=fopen("STUDENT","rt");
   if(stufile==NULL)
   {
      printf("\nError in reading STUDENT file");
      return;
   }
   fread(&s,sizeof(s),1,stufile);
   fclose(stufile);
}

//Write data to DATA file
void writeDataFile()
{
   FILE *datafile;
   datafile=fopen("STUDENT","wt");
   if(datafile==NULL)
   {
      printf("\nError in writing DATA file");
      return;
   }

   fwrite(&s,sizeof(s),1,datafile);
   fclose(datafile);
}

//Display data fetched from STUDENT file
void displayData()
{
   printf("\nData from STUDENT file");
   printf("\nRoll No : %d",s.rollno);
   printf("\nName    : %s",s.name);
}

void main()
{
  clrscr();
  readData();
  writeStudentFile();
  clearData();
  readStudentFile();
  writeDataFile();
  displayData();
}


No comments:

Post a Comment