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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *lireLigne2(char *dst,int tailleDep,FILE *flot)
{
int i=0;
char *chaine=NULL;
char *temp=NULL;
//char *verif=NULL;
char lettre=fgetc(flot);
chaine=calloc((tailleDep+1),sizeof(*chaine));
if (chaine==NULL)
{
printf("pb de calloc.\n");
exit(0);
}
if ((lettre=='\n'))
return NULL;
while((lettre!='\n'))
{
if (i<tailleDep)
{
chaine[i]=lettre;
chaine[i+1]='\0';
}
else
{
temp=chaine;
chaine=realloc(chaine,(strlen(temp)+2)*sizeof(*temp));
if (chaine==NULL)
printf("Pb de realloc1");
strcpy(chaine,temp);
chaine[strlen(chaine)]=lettre;
chaine[strlen(chaine)+1]='\0';
}
lettre=fgetc(flot);
i++;
};
dst=chaine;
printf("%s %p\n",dst,dst);
printf("%s %p\n",chaine,chaine);
return dst;
}
int main(void)
{
char *nom=NULL;
printf("Tapez votre nom :\n");
lireLigne2(nom,4,stdin);
//nom=lireLigne2(nom,4,stdin);
printf("%s %p\n",nom,nom);
if (nom==NULL)
{
printf("GROS PB\n");
exit(0);
}
printf("Vous êtes %s de taille %d.\n",nom,strlen(nom));
free(nom);
return EXIT_SUCCESS;
} |
Partager