C Programming Tutorial

Advertisements

Write a menu driven program in C to add, display, search, update and delete the student record



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

       void main( )
       {
        FILE  *fp, *ft;
        char  another,choice;
        clrscr();

        struct Student
        {

         int roll;
         char  name[40];
         int  age;
        };

        struct Student  stu;
        char  Studentname[40] ;
        char found='n';
        long int  recsize ;
        int roll;

        fp = fopen ( "E:\\Student.DAT", "rb+" ) ;

        if ( fp == NULL )
        {
         fp = fopen ( "E:\\Student.DAT", "wb+" ) ;

         if ( fp == NULL )
         {
	puts ( "Cannot open file" ) ;
	exit(0) ;
         }
        }

        recsize = sizeof (stu) ;

        while ( 1 )
        {
         clrscr() ;

      gotoxy ( 30, 6 ) ;
      printf ( "1. Add Records" ) ;
      gotoxy ( 30, 8 ) ;
      printf ( "2. List Records" ) ;
      gotoxy ( 30, 10) ;
      printf ( "3. Search Records" ) ;
      gotoxy ( 30, 12 ) ;
      printf ( "4. Modify Records" ) ;
      gotoxy ( 30, 14 ) ;
      printf ( "5. Delete Records" ) ;
      gotoxy ( 30, 16 ) ;
      printf ( "6. Exit" ) ;
      gotoxy ( 30, 18 ) ;
      printf ( "Your choice : " ) ;

  fflush (stdin) ;

  choice = getche();
  switch( choice )
  {
	case '1' :

	 fseek ( fp, 0 , SEEK_END ) ;
	 another = 'Y' ;

	 while ( another == 'Y' || another == 'y')
	 {
	  clrscr();
	  printf ( "\n\tEnter Roll : " ) ;
	  scanf ( "%d",&stu.roll);

	  printf ( "\n\tEnter Name : " ) ;
	  scanf ( "%s",&stu.name);

	  printf ( "\n\tEnter Age : " ) ;
	  scanf ( "%d",&stu.age);

	  fwrite ( &stu, recsize, 1, fp ) ;

	  printf ( "\n\tAdd another Record (Y/N) " ) ;
	  fflush ( stdin ) ;
	  another = getche( ) ;
	 }

	 break ;

	case '2' :

	 rewind ( fp ) ;
	  clrscr();
	  printf ( "\n\tRoll\tName\t\tAge");
	 while ( fread ( &stu, recsize, 1, fp ) == 1 )
	  printf ( "\n\t%d\t%s\t\t%d", stu.roll, stu.name, stu.age );

	  printf("\n\n\n\tPRESS ANY KEY TO EXIT.......");
	  getch();

	 break ;


	case '3' :

	 rewind ( fp ) ;
	 clrscr();
	 printf("\nEnter Roll for Search : ");
	 scanf("%d",&roll);

	 while ( fread ( &stu, recsize, 1, fp ) == 1 )
	 {

			if(roll == stu.roll)
			{
			  printf ( "\n\tRoll\tName\t\tAge");
			  printf ( "\n\t%d\t%s\t\t%d", stu.roll, stu.name, stu.age );

			  found='y';
			  break;
			  }
	  }

	  if(found=='n')
	  printf("\n\n\n\tNo Match Found.....");

	  printf("\n\n\n\tPRESS ANY KEY TO EXIT.......");
	  getch();

	 break ;



	case '4' :


	  rewind ( fp ) ;

	 clrscr();
	 printf("\nEnter Roll for Modify : ");
	 scanf("%d",&roll);

	  while ( fread ( &stu, recsize, 1, fp ) == 1 )
	  {


		if ( roll == stu.roll )
		{
			  printf ( "\n\tEnter New Roll : " ) ;
			  scanf ( "%d",&stu.roll);

			  printf ( "\n\tEnter New Name : " ) ;
			  scanf ( "%s",&stu.name);

			  printf ( "\n\tEnter New Age : " ) ;
			  scanf ( "%d",&stu.age);


		 fseek ( fp, - recsize, SEEK_CUR ) ;

		 fwrite ( &stu, recsize, 1, fp ) ;
		  printf ( "\nData Updated :  " ) ;
		 break ;


		}
	  }


	  printf("\n\n\n\tPRESS ANY KEY TO EXIT.......");
	  getch();

	 break ;

	case '5' :

	 another = 'Y' ;

	  clrscr();
	  printf ( "\nEnter Roll to Delete :  " ) ;
	  scanf ( "%d",&roll ) ;

	  ft = fopen ( "E:\\temp.DAT", "w" ) ;

	  rewind ( fp ) ;
	  while ( fread ( &stu, recsize, 1, fp ) == 1 )
	  {
		if ( roll != stu.roll )
		 fwrite ( &stu, recsize, 1, ft ) ;
	  }

	  fclose ( fp ) ;
	  fclose ( ft ) ;
	  remove ( "E:\\Student.DAT" ) ;
	  rename ( "E:\\temp.DAT", "E:\\Student.DAT" ) ;

	  fp = fopen ( "E:\\Student.DAT", "rb+" ) ;

	  printf("\n\n\n\tPRESS ANY KEY TO EXIT.......");
	  getch();


	 break ;

	case '6' :
	 fclose ( fp ) ;
	 exit(0) ;
  }
 }
}




Advertisement