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
|
#include <stdio.h>
#include <string.h>
static clean (char *s, FILE *fp)
{
char *p = strchr (s, '\n');
if (p != NULL)
{
*p=0;
}
else
{
int c;
if((c = fgetc (fp)) != '\n' && c != EOF)
{}
}
}
/* -ed-
void main()
main() retourne int. Toujours.
<a href="http://emmanuel-delahaye.developpez.com/notes.htm" target="_blank">http://emmanuel-delahaye.developpez.com/notes.htm</a>
*/
int main(void)
{
int ctrl,N1,N2,N3;
FILE *fic= fopen("notes.dat","w");
/* -ed- ajoute controle */
if (fic != NULL)
{
do
{
float moy;
char Prenom[20],Nom[20];
printf("1: saisie fiche - 2: Quit\n");
/* -ed-
ctrl = getchar();
mauvais choix de fonction de lecture.
<a href="http://emmanuel-delahaye.developpez.com/notes.htm" target="_blank">http://emmanuel-delahaye.developpez.com/notes.htm</a>
<a href="http://emmanuel-delahaye.developpez.com/notes.htm" target="_blank">http://emmanuel-delahaye.developpez.com/notes.htm</a>
*/
{
char s[4];
fgets(s, sizeof s, stdin);
clean(s, stdin);
ctrl = *s;
}
if (ctrl == '1')
{
printf("entrez Nom Prenom Note1 Note2 Note3\n");
/* -ed-
scanf("%s %s %d %d %d",Nom, Prenom, &N1, &N2, &N3);
mauvais choix de fonction de lecture.
*/
for(;;)
{
char line[128];
fgets(line, sizeof line, stdin);
clean(line, stdin);
{
int n = sscanf(line, "%s %s %d %d %d",Nom, Prenom, &N1, &N2, &N3);
if (n == 5)
{
moy = (N1 + N2 + N3)/3;
fprintf(fic,"\t%s\t%s\t%d\t%d\t%d\t%f\n",Nom,Prenom,N1,N2,N3,moy);
break;
}
else
{
printf ("input error\n");
}
}
}
}
/* -ed-
fflush(stdin);
N'existe pas en C standard. fflush() ne concerne que les flux sortants.
*/
}
while(ctrl != '2');
fclose(fic), fic=NULL;
}
return 0;
} |
Partager