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
| #include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct personne{
char nom[20];
char prenom[15];
char cin[10];
};
typedef struct personne personne;
int main(){
FILE *fichier,*out;
personne *liste;
personne tmp;
fichier=fopen("D:\\personne.txt","r");
if(fichier==NULL)
{
printf("impossible d'ouvrir le fichier");
exit(1);
}
int m=0;
while(fscanf(fichier,"%s%s%s",tmp.nom,tmp.prenom,tmp.cin)==3)
m++;
liste=(personne*)malloc(m*sizeof(personne));
fseek(fichier,0,0);
for(int i=0;i<m;i++)
fscanf(fichier,"%s%s%s",liste[i].nom,liste[i].prenom,liste[i].cin);
fclose(fichier);
out=fopen("D:\\pers.data","wb");
if(out==NULL)
{
printf("impossible d'ouvrir le fichier");
exit(1);
}
fwrite(liste,sizeof(personne),m,out);
fclose(out);
FILE *fic;
fic=fopen("D:\\pers.data","rb");
if(fic==NULL)
{
printf("impossible d'ouvrir le fichier");
exit(1);
}
fseek(fic,0,SEEK_END);
int n;
n=ftell(fic);
n=n/sizeof(personne);
printf("%d",n);
personne *T;
T=(personne*)malloc(n*sizeof(personne));
fread(T,sizeof(personne),n,fic);
fclose(fic);
for(int i=0;i<n;i++){
printf("\n%s",T[i].nom);
printf("\n%s",T[i].prenom);
printf("\n%s",T[i].cin);
}
system("pause");
return 0;
} |
Partager