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
| #include <stdio.h>
#include <stdlib.h>
struct s_etudiant
{
char nom[30];
char prenom[30];
unsigned short age;
char sex;
}
int main()
{
char mon_fichier[]="C:\\test.text";
FILE *fichier;
unsigned short i,n;
struct s_etudiant *p;
struct s_etudiant t[100];
char saisie[100];
fichier = fopen(mon_fichier,"w");
while (1)
{
printf("Entrez le nombre d' etudiants (jusqu'à 100): "); fflush(stdout);
fgets(saisie, 100, stdin);
if (sscanf(saisie, "%hu", &n) == 1 && n >0 && n < 100) break;
printf("Saisie incorrecte - recommencez\n");
}
for(i=0, p=t; i<n; i++, p++)
{
printf("\netudiant numero %hu\n",i+1);
printf("Entrez le nom de cet etudiant"); fflush(stdout);
fgets(p->nom, 30, stdin);
printf("Entrez le prenom de cet etudiant "); fflush(stdout);
fgets(p->prenom, 30, stdin);
while (1)
{
printf("Entrez l'age de cet etudiant "); fflush(stdout);
fgets(saisie, 100, stdin);
if (sscanf(saisie, "%hu", &p->age) == 1) break;
printf("Saisie incorrecte - recommencez\n");
}
while (1)
{
printf("Entrez le sexe de cet edutiant H pour homme F pour femme "); fflush(stdout);
fgets(saisie, 100, stdin);
if (sscanf(saisie, "%c", &p->sex) == 1 && (p->sex == 'H' || p->sex == 'F')) break;
printf("Saisie incorrecte - recommencez\n");
}
fprintf(fichier," %s\n%s\n%hu\n%c\n ", p->nom, p->prenom, p->age, p->sex);
}
fclose(fichier);
return 0;
} |
Partager