Student data bases management using files in C

begueradj 0 Tallied Votes 152 Views Share

Hi from Begueradj,
This program written in Turbo C simulates a student data bases using files.
Basic operations such as seeking for a given student, deleting, adding and sorting are implemented here.
Feel free to give me your points of view.
Thanks, Begueradj.

// This is done by Billal BEGIUERADJ
// Student File management 
// Code Built up in C
// beg.bill@yahoo.com
//Algeria //BEJAIA
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
typedef struct etudiant
			{int mat;char nom[20],prenom[20];float moy;};
void creation (FILE *x);
void insertion(FILE *x);
void affichage_moy(FILE *x);
void affichage_nom(FILE *x);
void affichage_sequen(FILE *x);
void recherche_nom(FILE *x);
void recherche_mat(FILE *x);
void suppression_moy(FILE *x);
void suppression_mat(FILE *x);
void calcul(FILE *x);
void affiche();
void s_menu1();
void s_menu2();
void s_menu3();
void cadre();
void music();
//*******************************************************
void main()
{
 FILE *x;
 //char t[30]={}
 int choix;
 start:
 textbackground(RED);
 clrscr();
 cadre();
 gotoxy(30,3);
 printf("** Le MENU PRINCIPAL **");
 gotoxy(30,4);
 printf("=======================");
 gotoxy(8,6);
 printf("1 -----> Création.");
 gotoxy(8,8);
 printf("2 -----> Insertion.");
 gotoxy(8,10);
 printf("3 -----> Listing (Affichage). ");
 gotoxy(8,12);
 printf("4 -----> Suppression. ");
 gotoxy(8,14);
 printf("5 -----> Recherche.");
 gotoxy(8,16);
 printf("6 -----> Calcul de la moyenne de la promotion");
 gotoxy(8,18);
 printf("7 -----> Quitter l\'application");
 gotoxy(30,21);
 printf("Votre choix SVP :   ");
 scanf("%d",&choix);
 clrscr();
 switch(choix)
	{
	case 1: gotoxy(25,2);
		printf("CREATION");
		creation(x);
		goto start;
	case 2: gotoxy(25,2);
		printf("L'INSERTION");
		insertion(x);
		goto start;
	case 3: gotoxy(25,2);
		printf("AFFICHAGE");
		s_menu2();
		goto start;
	case 4: gotoxy(25,2);
		printf("SUPPRISSION ");
		s_menu1();
		goto start;
	case 5: gotoxy(25,2);
		printf("RECHERCHE");
		s_menu3();
		goto start;
	case 6: gotoxy(25,2);
		printf("CALCUL ");
		calcul(x);
		goto start;
	case 7: gotoxy(25,2);
		gotoxy(22,8);
		textmode(C40);
		textcolor(YELLOW+BLINK) ;
		cprintf("\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t BEGUERADJ Billal\t\t\t\t\t\t");
		cprintf("\n\n\n\n\t\t\t\t\t\t\t\t beg.bill@yahoo.com\t\t\t\t\t\t");
		break;
		default :gotoxy(20,3);
		printf("CHOIX NON LISTE !  ");
		getch();
		goto start;
		}
 getch();
}
//************************************************************
void s_menu1(){ FILE *x;
			int choix1;
			cadre();
			textcolor(2);
			gotoxy(15,8);
			printf("1 ------> SUPPRESSION DES ETUDIANTS AYANTS UNE MOYENNE <10");
			gotoxy(15,10);
			printf("2 ------>SUPPRESSION DES ETUDAINTS PAR MATRICULE");
			gotoxy(30,15);
			printf("DONNER VOTRE CHOIX SVP ?  ");
			scanf("%d",&choix1);
			switch(choix1) {
				 case 1: clrscr();
					 gotoxy(5,2);
					 printf("SUPPRESSION DES ETUDAINTS AYANTS UNE MOYENNE <10 ");
					 break;
				 case 2: clrscr();
					 gotoxy(5,2);
					 printf("SUPPRESSION DES ETUDIANTS PAR MATRICULE ");
					 suppression_mat(x);
					 break;
				 default :gotoxy(10,20);
					  printf("VOTRE CHOIX N'EXISTE PAS DANS LE MENU !");
					  getch();
					  break;
				  }
 }
//*********************************************************************
void s_menu2(){
			 FILE *x;
			 int choix2;
			 cadre();
			 textcolor(6);
			 gotoxy(8,8);
			 printf("1 ------> AFFICHAGE DES ETUDIANTS PAR ORDRE ALPHABITIQUE");
			 gotoxy(8,10);
			 printf("2 ------> AFFICHAGE DES ETUDAINTS PAR ORDRE CROISSANT DE LEURS MOYENNE");
			 gotoxy(8,12);
			 printf("3 ------> AFFICHAGE DES ETUDAINTS PAR ORDRE DE LEURS ARRIVES (sequenciel)");
			 gotoxy(20,20);
			 printf("VOTRE CHOIX SVP :   ");
			 scanf("%d",&choix2);
			 switch(choix2) {
				 case 1: printf("\nL'AFFICHAGE DES ETUDAINTS PAR ORDRE ALPHABITIQUE DE LEURS NOM");
				 affichage_nom(x);
				 break;
				 case 2: printf("\nL'AFFICHAGE DES ETUDIANTS PAR OREDRE CROISSANT DE LEURS MOYENNE ");
					 affichage_moy(x);
					 break;
					 case 3: printf("\nL'AFFICHAGE DES ETUDIANTS PAR OREDRE DE LEURS ARRIVES ");
					 affichage_sequen(x);
					 break;
				 default :printf("\nVOTRE CHOIX N'EXISTE PAS DANS LE MENU !");break;
			 }
 }
//*********************************************************************
void s_menu3(){
  FILE *x;
  int choix3;
			cadre();
			textcolor(2);
			gotoxy(15,8);
			printf("1 ------> RECHERCHE D'ETUDIANT IDENTIFIE PAR SON NOM");
			gotoxy(15,10);
			printf("2 ------> RECHERCHE D'UN ETUDAINTS IDENTIFIE PAR SON MATRICULE");
			gotoxy(25,15);
			printf("VOTRE CHOIX SVP :   ");
			scanf("%d",&choix3);
			clrscr();
			cadre();
			switch(choix3) {
				 case 1:
				 gotoxy(5,2);
				 printf("LA RECHERCHE D'UN ETUDAINT IDENTIFIE PAR SON NOM");
				 recherche_nom(x);
				 break;
			    case 2:
			    gotoxy(5,2);
				 printf("LA RECHERCHE D'UN ETUDIANT IDENTIFIE PAR MATRICULE ");
				 recherche_mat(x);
					 break;
				 default :gotoxy(5,4); printf("VOTRE CHOIX N'EXISTE PAS DANS LE MENU !");
				 getch();
				 break;
				 }
 }
//*********************************************************************
void creation(FILE *x)
{
 etudiant e ;
 int n,k ;
 char r;
 textcolor(2);
 cadre();
 gotoxy(20,4);
 printf("ALERTE");
 gotoxy(5,6);
 printf(" CETTE FONCTION VA CREER UN NOUVEAU FICHIER");
 gotoxy(10,8);
 printf("SI LE FICHIER EXISTE DEJA");
 gotoxy(5,10);
 printf("VOUS RESQUEZ DE PERDRE LES INFORMATIONS QUI CONTIENT");
 music();
 gotoxy(5,20);
 printf("Etes vous sur de vouloir cr‚er un nouveau fichier ? o/n :  ");
 r=getch();
 if (r=='n') {goto fin;}
 //sleep(7);
 clrscr();
 cadre();
 gotoxy(5,4);
 textcolor(3);
 printf("Donnez le nombre d'etudiant a ecrire dans le fichier : ");
 scanf("%d",&n);
 clrscr();
 x=fopen("etudiant.dat","w");
 textcolor(11);
 for (k=0;k<n;++k)
		{
	clrscr();
	cadre();
	gotoxy(25,5);
	printf("L'etudiant Nø%d",k+1);
	affiche();
	gotoxy(34,8);
	scanf("%d",&e.mat);
	gotoxy(34,10);
	scanf("%s",e.nom);
	gotoxy(34,12);
	scanf("%s",e.prenom);
	gotoxy(34,14);
	scanf("%f",&e.moy);
	fwrite(&e,sizeof(etudiant),1,x);
	//getch();
		 }
 fclose(x);
 fin:
}
//*************************************************************
void insertion (FILE *x)
{
 int  nbr,j,n=0;
 etudiant e;
 cadre();
 gotoxy(5,4);
 textcolor(3);
 printf("Donnez le nombre d'etudiant a insérer dans le fichier : ");
 scanf("%d",&nbr);
 clrscr();
 cadre();
 textcolor(11);
 x=fopen("etudiant.dat","r+");
 while(!feof(x))
		 {
		 fread(&e,sizeof(etudiant),1,x); n++;
		 }
 for (j=0;j<nbr;++j)
		{
		 clrscr();
		 cadre();
		 gotoxy(25,5);
		 printf("L'etudiant Nø%d",n++);
		 affiche();
		 gotoxy(34,8);
		 scanf("%d",&e.mat);
		 gotoxy(34,10);
		 scanf("%s",e.nom);
		 gotoxy(34,12);
		 scanf("%s",e.prenom);
		 gotoxy(34,14);
		 scanf("%f",&e.moy);
		 fwrite(&e,sizeof(etudiant),1,x);
		// getch();
		}
 fclose(x);
 }
//***********************************************************
void affiche()
{
 textcolor(7);
 gotoxy(5,8);
 printf("La matricule de l'‚tudiant  ");
 gotoxy(5,10);
 printf("Le nom de l'‚tudiant        ");
 gotoxy(5,12);
 printf("Le prenom de l'etudiant     ");
 gotoxy(5,14);
 printf("La moyenne g‚n‚rale         ");
}
//***********************************************************
void affichage_sequen(FILE *x)
{
 etudiant e;
 int i=1;
 x=fopen("etudiant.dat","r");
 textcolor(13);
 fread(&e,sizeof(etudiant),1,x);
 do
  {
	gotoxy(25,5);
	printf("L'ETUDIANT Nø %d ",i);
	clrscr();
	cadre();
	affiche();
	gotoxy(34,8);
	printf("%d",e.mat);
	gotoxy(34,10);
	printf("%s",e.nom);
	gotoxy(34,12);
	printf("%s",e.prenom);
	gotoxy(34,14);
	printf("%2.2f",e.moy);
	fread(&e,sizeof(etudiant),1,x);
	getch(); i++;
	}  while (!feof(x));
 fclose(x);
 }
//***********************************************************************
void affichage_moy(FILE *x)
 {
 etudiant t[20],e;
 int i,j,k=0;
 x=fopen("etudiant.dat","r");
 fread(&e,sizeof(etudiant),1,x);
 do{
	t[k]=e ;
	fread(&e,sizeof(etudiant),1,x);
	k++;
 } while (!feof(x));
 fclose(x);
 for(i=0;i<k;i++){
	for (j=i+1;j<k;j++)
		{
		 if(t[i].moy>=t[j].moy)
			{
			 e=t[i];
			 t[i]=t[j];
			 t[j]=e;
			}
		}
	textcolor(13);
	gotoxy(25,5);
	printf("L'ETUDIANT Nø %d ",i);
	clrscr();
	cadre();
	affiche();
	gotoxy(34,8);
	printf("%d",t[i].mat);
	gotoxy(34,10);
	printf("%s",t[i].nom);
	gotoxy(34,12);
	printf("%s",t[i].prenom);
	gotoxy(34,14);
	printf("%2.2f",t[i].moy);
	getch();
	}
 }
//**********************************************************************/
void affichage_nom(FILE *x)
{
 etudiant t[20],e;
 int i,j,k=0;
 cadre();
 gotoxy(5,5);
 printf("VOUS AVEZ CHOISI L'AFFICHAGE DES ETUDAINTS PAR ORDRE ALPHABITIQUE ");
 gotoxy(15,6);printf("DE LEURS NOM");
 sleep(3);
 x=fopen("etudiant.dat","r");
 fread(&e,sizeof(etudiant),1,x);
 do{
	 t[k]=e ;
	 fread(&e,sizeof(etudiant),1,x);
	 k++;
	} while (!feof(x));
 fclose(x);
for(i=0;i<k;i++){
		for (j=i+1;j<k;j++){
			if (strcmp(t[i].nom,t[j].nom)>0){
				  e=t[i];
				  t[i]=t[j];
				  t[j]=e;
				  }
				  }
	clrscr();
	cadre();
	affiche();
	gotoxy(34,8);
	printf("%d",t[i].mat);
	gotoxy(34,10);
	printf("%s",t[i].nom);
	gotoxy(34,12);
	printf("%s",t[i].prenom);
	gotoxy(34,14);
	printf("%2.2f",t[i].moy);
	getch();
	}
}
/***********************************************************************/
void calcul(FILE *x){
 etudiant e;
 float m=0 ;
 int i=0;
 x=fopen("etudiant.dat","r");
 fread(&e,sizeof(etudiant),1,x);
 while (!feof(x)){
		m=m+e.moy ;
		i++;
		fread(&e,sizeof(etudiant),1,x);
		}
 if(i!=0){
	m=m/i;
	}
 cadre();
 gotoxy(5,7);
 printf("La moyenne de la promotion est:   %2.2f",m);
 getch();
 }
/**************************************************************************/
void recherche_nom(FILE*x){
 etudiant e;
 char nom2[20];
 int i=0;
 gotoxy(7,4);
 printf("donner le nom de l'etudiant a chercher ");
 scanf("%s",nom2);
 x=fopen("etudiant.dat","r");
 while(!feof(x)){
	 fread(&e,sizeof(etudiant),1,x);
	 if(strcmp(e.nom,nom2)==0)   {
	 i=1;
	 affiche();
	 gotoxy(34,8);
	 printf("%d",e.mat);
	 gotoxy(34,10);
	 printf("%s",e.nom);
	 gotoxy(34,12);
	 printf("%s",e.prenom);
	 gotoxy(34,14);
	 printf("%2.2f",e.moy);
	 fread(&e,sizeof(etudiant),1,x);
	}
	}
 if(i==0){
 gotoxy(25,10);
 printf("CET ETUDIANT N'EXISTE PAS");}
 getch();
 }
//**********************************************************************
void recherche_mat(FILE*x){
 etudiant e; int matr;
 int i=0;
 gotoxy(7,4);
 printf("donner le matricule de l'etudiant a chercher ");
 scanf("%d",&matr);
 x=fopen("etudiant.dat","r");
 while(!feof(x)){
	 fread(&e,sizeof(etudiant),1,x);
	 if(e.mat==matr)   {
	 i=1;
	 affiche();
	 gotoxy(34,8);
	 printf("%d",e.mat);
	 gotoxy(34,10);
	 printf("%s",e.nom);
	 gotoxy(34,12);
	 printf("%s",e.prenom);
	 gotoxy(34,14);
	 printf("%2.2f",e.moy);
	 fread(&e,sizeof(etudiant),1,x);
	 getch();
	 }
	}
 if(i==0)
	{gotoxy(25,10);
	printf("CET ETUDIANT N'EXISTE PAS");}
	getch();
 }
 //*************************************************************
  /*------------------- DESSIN DE CADRE-------------------*/
void cadre(){
int i;
clrscr();
for(i=3;i<78;i++){
	gotoxy(i,1);
	printf("");
	gotoxy(i,22);
	printf("*");
	gotoxy(i,25);
	printf("");}
 for (i=0;i<26;i++){
		gotoxy(3,i);
		printf("");
		gotoxy(78,i);
		printf(""); }
 gotoxy(8,24);
 printf("");
}
/************************************************************/
void music(){
 int i;
for(i=0;i<7;i++){
	sound(100*i);
	delay(300*i);
	nosound();    }
	}
/************************************************************/
 void suppression_mat(FILE *x){
 FILE *y;
 etudiant e;
 int mat ,pos;
 char re;
  cadre();
 //sleep(2);
  //clrscr();
 gotoxy(6,4);
 printf("Donnez la matricule de l'etudiant a suppremer ?:   ");
 scanf("%d",&mat);
 gotoxy(5,8);
 printf("etes vous sur de vouloir supprimer l'etudiant de matricule %d  ? o/n",mat);
 re=getch();
 if(re=='n')goto not_del;
 y=fopen("etud.dat","w");
 x=fopen("etudiant.dat","r");
// i=0;
 while(!feof(x)){
	 fread(&e,sizeof(etudiant),1,x);
	 if(e.mat!=mat)   {
	 //i++;
	 fwrite(&e,sizeof(etudiant),1,y);
	 }
  }
  fclose(x);
  fclose(y);
  x=fopen("etudiant.dat","w");
  y=fopen("etud.dat","r");
  while(!feof(y)){
  fread(&e,sizeof(etudiant),1,y);
  fwrite(&e,sizeof(etudiant),1,x);
  }
 fclose(x);
 fclose(y);
 not_del:
 }
Bahgia 0 Newbie Poster

Cannot compile using Dev C++. Why?

Member Avatar for begueradj
begueradj

Hello Bahgia,

Dev C++ has not the option of running programs under DOS console, this is why my program does not run on Dev C++.

If you want to run it under Dev C++ all those functions that run under DOS console such as textcolor(int).

If you have any compiler which enables you to run programs both under Windows and Dos consoles, you have just to choose the option "run under Dos" and it will work fine.

I have tried this program under Borland C++ 4.52 (under Dos) and it rteally worked also very fine.

Greetings from Begueradj.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.