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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* nettoyeur */
static void clean (char *s, FILE *fp)
{
/* search ... */
char *p = strchr (s, '\n'); /* <string.h> */
if (p != NULL)
{
/* ... and kill */
*p = 0;
}
else
{
/* purge */
int c;
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
}
}
}
int main (void)
{
#define LEN_NAME 18
#define LEN_AGE 3
int end = 0;
while (!end)
{
char nom[LEN_NAME + 2];
int age;
printf("Nom (or ""<enter> to quit) : ");
fflush (stdout);
fgets(nom, LEN_NAME + 1, stdin);
clean(nom, stdin);
end = *nom == 0;
if (end)
{
/* premiere fois de ma vie que j'utilise cet engin... */
continue;
}
{
char *pend;
do
{
char a[LEN_AGE + 2];
printf("Age : ");
fflush (stdout);
fgets(a, LEN_AGE + 1, stdin);
clean(a, stdin);
age = (int) strtol(a, &pend, 10);
}
while (*pend != 0);
}
{
char requete[128];
sprintf(requete, "insert into t1 values (\'%s\', %d)", nom, age);
printf("req:%s\n", requete);
/* ... */
}
}
return 0;
} |
Partager