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
| #include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct profile profile;
struct profile
{
int id;
char nom[20];
char prenom[30];
int numtel;
char email[20];
};
int main(void) {
FILE *pf;
char chaine[100],x[20]="osiris",x2[30]="ffff",x4[20]="dfodpp";
int i,TAILLE_MAX=0,x3=25;
pf = fopen("C:\\test.txt","r+");
do
{
if (fgetc(pf)=='\n')
{
TAILLE_MAX++;
}
}while(!feof(pf));
rewind(pf);
profile * tab = NULL;
tab = (profile *) malloc (TAILLE_MAX * sizeof(profile));
if( tab == NULL )
{
fprintf(stderr,"Allocation impossible");
exit(EXIT_FAILURE);
}
if (pf != NULL)
{
i=1;
do
{
fgets(chaine,100,pf);
sscanf(chaine,"| %d | %s | %s | %d | %s |",&tab[i].id,tab[i].nom,tab[i].prenom,&tab[i].numtel,tab[i].email);
i++;
}while(!feof(pf));
}
for(i=1;i<=TAILLE_MAX;i++) printf("| %d | %s | %s | %d | %s |\n",tab[i].id,tab[i].nom,tab[i].prenom,tab[i].numtel,tab[i].email);
TAILLE_MAX++;
profile *temp = NULL;
temp = (profile *) realloc (tab,TAILLE_MAX * sizeof(profile));
if ( temp == NULL )
{
fprintf(stderr,"Reallocation impossible");
free(tab);
exit(EXIT_FAILURE);
}
else {tab = temp;}
free(temp);
temp = NULL;
strcpy(tab[i].nom,x);
strcpy(tab[i].prenom,x2);
tab[i].numtel=x3;
strcpy(tab[i].email,x4);
fseek(pf,0,SEEK_END);
fprintf(pf,"| %d | %s | %s | %d | %s |\n",i,tab[i].nom,tab[i].prenom,tab[i].numtel,tab[i].email);
fclose(pf);
for(i=1;i<=TAILLE_MAX;i++) printf("| %d | %s | %s | %d | %s |\n",i,tab[i].nom,tab[i].prenom,tab[i].numtel,tab[i].email);
getch();
return 0;
} |
Partager