Bonjour à tous,
Je suis programmeur débutant en c,j'étudie en ce moment les fichiers.
Le problème que je voudrais vous soumettre est l'écriture dans un fichier,d'un tableau de structure..
je vous présente mon code.
En premier ma bibliothèque contact.h
Ici le main.c
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 #include<stdio.h> #include<stdlib.h> #include<string.h> #define Max 1 #define TAILLE_MAX 1000 int verif(int *test); typedef struct Personne Personne; struct Personne { char nam[20]; char pre[15]; int age; int tel; int id; }; void cREATCONTACT(FILE *fiche,Personne *info,int taille); void controlsaisie(Personne *tab,int tal); void lIRECONTACT(FILE *fiche,Personne *info,int taille); void rECHERCHECONTACT(FILE *fiche,Personne *info,int taille);
Les fonctions
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 #include"contact.h" int main() { FILE *Fichier=NULL; Personne contact[Max]; int choix; int *pointchoix=&choix; while(choix!=4) { printf("*****Bienvennue dans la gestion des contacts***\n\a"); system("cls"); printf("****** MENU******\n");//menu de gestion system("pause"); switch(verif(pointchoix)) { case 1: controlsaisie(contact,Max); cREATCONTACT(Fichier,contact,Max); break; case 2: lIRECONTACT(Fichier,contact,Max); break; case 3: rECHERCHECONTACT(Fichier,contact,Max); break; case 4: printf("Au revoir et merci \t"); system("pause"); break; } } return 0; } int verif(int *test)//Fonction de gestion du menu { int x; x=0; while(x<1 || x>4) { system("cls"); printf("1-Creation d un contact\a\n");//Controle printf("\n\n"); printf("2-Visualisation des contacts\n"); printf("\n\n"); printf("3-Recherche contact\n"); printf("\n\n"); printf("4-Quitter\n"); printf("\n\n"); printf("Faites votre choix svp\?\n"); fflush(stdin); scanf("%d",&x); system("pause"); } *test=x; return *test; }
Mon problème se situe au niveau de l'écriture du tableau de structure ,l'écriture de celui ci dans le fichier.Pouvez vous m'aider.
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
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
77
78 #include"contact.h" void cREATCONTACT(FILE *fiche,Personne *info,int taille)//creation fichier { int i; //char ligne[80]; //for(i=0;i<=taille;i++) //{ fiche=fopen("c:\\Codblock\\Contacttel\\Contact.txt","a+"); if(fiche!=NULL) { system("cls"); fwrite(info,sizeof(info[0].id),sizeof(info)/sizeof(info[0].id),fiche); fclose(fiche); } else { printf("erreur"); } //} } void controlsaisie(Personne *tab,int tal) { int j,k; char saisie[11]; // for(j=0;j<=tal;j++) //{ while(1) { printf("Rentrez ID du contact\t:\n"); fflush(stdin); fgets(saisie,11,stdin); if(sscanf(saisie,"%3d", &k)==1)//saisie controle age { tab->id=k; break; } printf("Erreur rentrez un identifiant svp\n"); } printf("Rentrez le nom du contact %d:\t",j+1); fflush(stdin);//vider tampon fgets(tab[0].nam,20,stdin); printf("Rentrez le prenom du contact:\t");//Saisie nom et prenom fgets(tab[0].pre,15,stdin); while(1) { printf("Rentrez le telephone du contact\t:\n"); fflush(stdin); fgets(saisie, 11, stdin); if(sscanf(saisie,"%11d",&k)==1) { tab[0].tel=k;//saisie telephone break; } printf("Erreur rentrez un numero de telephone"); } while(1) { printf("Rentrez l age du contact\t:\n"); fflush(stdin); fgets(saisie,11,stdin); if(sscanf(saisie,"%3d", &k)==1)//saisie controle age { tab[0].age=k; break; } printf("Erreur rentrez un nombre pour l age svp\n"); } // } }
Toutes remarque sur mon code ou ma réflexion algorithmique est la bienvenue.
Merci d avance
Partager