hello,
j'ai un probleme lorsque j'écris ou lis je sais pas très bien dans un fichier avec ces 2 fonctions..
En fait il s'agirait d'écrire une structure où tout les éléments de la structure sont de taille et de type différents... je donne mon code pour que ce soit plus clair
le code pour l'écriture
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdio.h> main(){ FILE * fichier; char ligne[1024]; int nb; char *ptr; struct magasin{ short fournir; char nom[50]; char rue[100]; int numero; int code_postal; char commune[50]; char tel[11]; char fax[11]; }magasin ={0,"","",0,0,"",0,0}, *mag; fichier = fopen("test.txt","w"); if(fichier == NULL){ printf("\nErreur création de %s\n", "fichier.xt"); exit(2); } printf("fourni num"); magasin.fournir=strtol(gets(ligne),&ptr,10); printf("nom mag"); gets(magasin.nom); printf("rue mag"); gets(magasin.rue); printf("numero rue"); magasin.numero=strtol(gets(ligne),&ptr,10); printf("cp"); magasin.code_postal=strtol(gets(ligne),&ptr,10); printf("commune"); gets(magasin.commune); printf("tel"); gets(magasin.tel); printf("fax"); gets(magasin.fax); nb= fwrite(mag,8*100*sizeof(char),8,fichier); printf("%d element ecrit",nb); fclose(fichier); getchar(); }
et ici pour la lecture, et donc ici ce qu'il lit na rien a voir avec ce qu'on a écrit dans le fichier
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdio.h> #include <stdlib.h> main(){ FILE * fichier; struct magasin{ short fournir; char nom[50]; char rue[100]; int numero; int code_postal; char commune[50]; char tel[11]; char fax[11]; }magasin ={0,"","",0,0,"",0,0}, *mag; fichier = fopen("test.txt","rb"); if( fichier==NULL ) { printf("\nErreur en lecture de %s\n", "test.txt"); exit(2); } fread(mag,8*100*sizeof(char),8,fichier); printf("%d\n",magasin.fournir); printf("%s\n",magasin.nom); printf("%s\n",magasin.rue); printf("%d\n",magasin.numero); printf("%d\n",magasin.code_postal); printf("%s\n",magasin.commune); printf("%d\n",magasin.tel); printf("%d\n",magasin.fax); fclose(fichier); getchar(); }
Partager