1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
typedef struct
{
char nom[20];
char prenom[20];
int code;
}etudiant;
void main()
{ char choix;
void saisie(), suppression(),edition(),tri();
//affichage de menu
do
{
puts("\t____________________________");
puts("\t gestion des etudiants");
puts("\t____________________________");
puts("\t\t 1-saisie");
puts("\t\t 2-suppression");
puts("\t\t 3-edition");
puts("\t\t 4-tri");
puts("\t\t 5-fin");
printf(" choix: ");
do
choix=getchar();
while(( choix<'1')||(choix>'5'));
switch(choix)
{
case '1':saisie();break;
case '2':suppression();break;
case '3':edition();break;
case '4':tri();break;
case '5':printf("au revoir!\n");break;
}
}while(choix!='5');
}
////////////////////////////////////////////////////////////////////////////////////////////
void saisie()
{ etudiant e;
char reponse;
FILE*f;
f=fopen("etudiant.txt","a+");
if(f!=NULL)
do
{
printf("entrer le nom de votre etudiant\n");
scanf("%s",e.nom);
printf("entrer le prenom de votre etudiant\n");
scanf("%s",e.prenom);
printf("entrer le code de votre etudiant\n");
scanf("%s",&e.code);
fwrite(&e,sizeof(e),1,f);
printf("voulez vous continuez?[o/n]");
do
reponse=getchar();
while((toupper(reponse)!='N')&&( toupper(reponse)!='O'));
puts("\n");
}
while(toupper(reponse)!='N');
fclose(f);
}
//////////////////////////////////////////////////////////////////////////////////////////////
void suppression()
{
char ANCIEN[30], NOUVEAU[30];
FILE *INFILE, *OUTFILE;
char NOM_SUPPR[30];
etudiant e;
/* Ouverture de l'ancien fichier en lecture */
do{
printf("Nom de l'ancien fichier : ");
scanf("%s", ANCIEN);
INFILE = fopen(ANCIEN, "r");
if (!INFILE)
printf("\aERREUR: Impossible d'ouvrir "
"le fichier: %s.\n", ANCIEN);
}
while (!INFILE);
/* Ouverture du nouveau fichier en écriture */
do
{
printf("Nom du nouveau fichier : ");
scanf("%s", NOUVEAU);
OUTFILE = fopen(NOUVEAU, "w");
if (!OUTFILE)
printf("\aERREUR: Impossible d'ouvrir "
"le fichier: %s.\n", NOUVEAU);
}
while (!OUTFILE);
printf("Enregistrement a supprimer : ");
scanf("%s",NOM_SUPPR);
while (!feof(INFILE))
{
fscanf(INFILE, "%s\n", e.nom);
if (strcmp(e.nom, NOM_SUPPR) != 0)
fprintf(OUTFILE, "%s\n", e.nom);
}
/* Fermeture des fichiers */
fclose(OUTFILE);
fclose(INFILE);
remove("etudiant.txt");
rename("eleve.txt","etudiant.txt");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void edition()
{ int i;
etudiant e;
FILE*f;
f=fopen("etudiant.txt","r");
if(f!=NULL)
{ i=1;
while(fread(&e,sizeof(e),1,f)&&!feof(f))
{ puts("\n");
printf("la personne numero %d est:\n",i);
printf("le nom est %s:\n",e.nom);
printf("le prenom est %s:\n",e.prenom);
printf("le code est %d:\n",e.code);
i++;
}
fseek(f,0,2);
printf("le fichier contient %d element \n",ftell(f)/sizeof(e));
}
fclose(f);
}
///////////////////////////////////////////////////////////////////////////////////////////
void tri()
{
printf("hi");
} |
Partager