Précédent   Forum du club des développeurs et IT Pro > C et C++ > C
C Forum d'entraide technique sur le langage C. Avant de poster -> F.A.Q. C, Avant de poster.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 09/01/2013, 15h55   #1
Raveen
Invité de passage
 
Homme
Étudiant
Inscription : décembre 2012
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Paris (Île de France)

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : décembre 2012
Messages : 4
Points : 0
Points : 0
Par défaut [Demande d'aide]Base de donnée en C.

Bonjour,

Voilà, je travaille sur un mini-projet qui gère une petite base de donnée en C.
Et voilà je crée un topic ici pour que les personnes présentes sur ce forum puissent m'apporter de l'aide, des conseils, ou mêmes des critiques constructives.
Je mettrais a jour a chaque modification sur ce post là.
J'utilise Code::Blocks.

Merci.

[09/01/2013]: Je bloque actuellement sur la fonction affichage qui m'affiche bien tout sauf le vector DATA.
Avez vous des conseils sur comment je devrais m'y prendre pour remedier a ce problèmes

printf("%s ",a.tables->tab[i].champs->tab[j].DATA->tab[l]);} à la ligne 210.


Le code source:

Code :
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TYPE char;
 
void *myalloc(size_t n){
void *ret = malloc(n);
if(ret == NULL){
printf("Erreur de Malloc");
exit(1);
}
return ret;
}
 
void *my_realloc(void *p, size_t size) {
  p = realloc(p, size);
  if (p == NULL) {
    fprintf (stderr, "Erreur : impossible de reallouer la mémoire\n");
    exit(-1);
  }
  return p;
}
 
 
 
/*-----------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------*/
 
 
typedef struct _vector{
char *tab;
int n;
int cap;
} *vector;
 
typedef struct _champ{
char entete[100]; 
int type_data;
vector DATA;
}champ;
 
typedef struct _cvector{
champ *tab;
int n;
int cap;
} *cvector;
 
 
typedef struct table{
char nom[100]; //nom de la table
cvector champs;
}table;
 
typedef struct _tvector{
table *tab;
int n;
int cap;
} *tvector;
 
 
typedef struct _database{
char nom[100];
table dtable;
tvector tables;
}database;
 
 
 
tvector new_tvector(){
tvector p= myalloc(sizeof(*p));
p->tab= myalloc(25 * sizeof(table));
p->cap=25;
p->n = 0;
return p;
}
 
cvector new_cvector(){
cvector r= myalloc(sizeof(*r));
r->tab= myalloc(25 * sizeof(champ));
r->cap=25;
r->n = 0;
return r;
}
 
vector new_vector(){
vector v=myalloc(sizeof(*v));
v->tab= myalloc(150*sizeof(char));
v->n=0;
v->cap=150;
return v;
}
 
champ new_champs(char *cname, int typ){
champ nouv;
strcpy(nouv.entete,cname);
nouv.type_data= typ;
nouv.DATA= new_vector();
return nouv;
}
 
table new_table(char *tname){
table nouv ;
strcpy(nouv.nom,tname);
nouv.champs = new_cvector();
return nouv;
}
 
database new_database(char *dname){
database nouv;
strcpy(nouv.nom,dname);
 
nouv.tables = new_tvector();
return nouv;
}
 
 
 
 
 
 
/* LES FONCTIONS RESERVE */
 
void reserve(vector v, int cap) {
  if (v->cap == 0) {
    v->tab = myalloc (cap *sizeof (*v->tab));
    v->cap = cap;
    return;
  }
  v->tab = my_realloc(v->tab, cap *sizeof (*v->tab));
  v->cap = cap;
  if (v->n > cap)
    v->n=cap;
}
 
 
void treserve(tvector v, int cap) {
  if (v->cap == 0) {
    v->tab = myalloc (cap *sizeof (*v->tab));
    v->cap = cap;
    return;
  }
  v->tab = my_realloc(v->tab, cap *sizeof (*v->tab));
  v->cap = cap;
  if (v->n > cap)
    v->n=cap;
}
 
void creserve(cvector v, int cap){
    if (v->cap == 0) {
    v->tab = myalloc (cap *sizeof (*v->tab));
    v->cap = cap;
    return;
  }
  v->tab = my_realloc(v->tab, cap *sizeof (*v->tab));
  v->cap = cap;
  if (v->n > cap)
    v->n=cap;
}
/*LES FONCTIONS PUSHBACK*/
 
void push_back(vector v, char DATA){
  if (v->cap == v->n) {
    if (v->cap == 0)
      reserve (v,1);
    else
      reserve (v, 2 * v->cap);
  }
  v->tab[v->n] = DATA;
  v->n++;
}
 
void tpush_back(tvector a, table b){
  if(a->cap == a->n) {
    if (a->cap == 0)
       treserve (a, 1);
    else
       treserve (a, (2 * a->cap));
  }
  a->tab[a->n] = b;
  a->n++;
  }
 
void cpush_back(cvector a, champ t){
if(a->cap == a->n) {
    if (a->cap == 0)
       creserve (a, 1);
    else
       creserve (a, (2 * a->cap));
  }
  a->tab[a->n] = t;
  a->n++;
  }
 
int sizetable(database a){
 
        return a.tables->tab->champs->n;
    }
 
 
void affiche(database a){
     int i,j,l;
     printf("%s \n", a.nom);
     for(i=0;i<2;i++){
    printf("%s \n",a.tables->tab[i].nom);
    for(j=0;j<3;j++){
    printf("%s ",a.tables->tab[i].champs->tab[j].entete);
 
     for(l=0;l<4;l++){
     printf("%s ",a.tables->tab[i].champs->tab[j].DATA->tab[l]);}
 
}}}
 
 
int main(){
    database a = new_database("Data Etudiant");
    table un=new_table("Etudiants");
    a.tables->tab[0]=un;
    champ id=new_champs("ID",0);
    un.champs->tab[0] = id;
    id.DATA->tab[0]= "1";
    id.DATA->tab[1]= "2";
    id.DATA->tab[2]= "3";
    id.DATA->tab[3]= "4";
    champ nom=new_champs("Nom",1);
    un.champs->tab[1] = nom;
    nom.DATA->tab[0]= "VLAD";
    nom.DATA->tab[1]= "RAVEEN";
    nom.DATA->tab[2]= "CIOBI";
    nom.DATA->tab[3]= "SARKO";
    champ prenom= new_champs("Prenom",1);
    un.champs->tab[2] = prenom;
    prenom.DATA->tab[0]= "JEAN";
    prenom.DATA->tab[1]= "PERERA";
    prenom.DATA->tab[2]= "STEFAN";
    prenom.DATA->tab[3]= "ZY";
 
    table deux = new_table("Notes");
    a.tables->tab[1] = deux;
    affiche(a);
}
Les Erreurs de compilation:
Citation:
Compiling: C:\Users\Raveen\Downloads\main.c
C:\Users\Raveen\Downloads\main.c: In function 'main':
C:\Users\Raveen\Downloads\main.c:256: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:257: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:258: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:259: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:262: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:263: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:264: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:265: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:268: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:269: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:270: warning: assignment makes integer from pointer without a cast
C:\Users\Raveen\Downloads\main.c:271: warning: assignment makes integer from pointer without a cast
Linking console executable: C:\Users\Raveen\Downloads\main.exe
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 12 warnings
Raveen est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2013, 16h28   #2
diogene
Responsable Modération
 
Avatar de diogene
 
Homme Patrick Gonord
Enseignant Chercheur
Inscription : juin 2005
Messages : 5 434
Détails du profil
Informations personnelles :
Nom : Homme Patrick Gonord
Localisation : France, Essonne (Île de France)

Informations professionnelles :
Activité : Enseignant Chercheur
Secteur : Enseignement

Informations forums :
Inscription : juin 2005
Messages : 5 434
Points : 12 952
Points : 12 952
id.DATA->tab[0] est un char (donc un entier), alors que "1" est un char*.

Et pareil pour les autres.

Alors, ce code est certainement faux. Pour corriger, tout dépend de ce que tu veux faire avec id.DATA->tab (et les autres).
__________________
Publication : Concepts en C

Mon avatar : Glenn Gould

--------------------------------------------------------------------------
Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !
diogene est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2013, 17h20   #3
Raveen
Invité de passage
 
Homme
Étudiant
Inscription : décembre 2012
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Paris (Île de France)

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : décembre 2012
Messages : 4
Points : 0
Points : 0
Alors oui je viens de remarquer mon erreur ce que je veux faire est assez compliqué a expliquer mais en quelques sortes mon id doit contenir:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
 
typedef struct _champ{
char entete[100]; // Une entête
int type_data; // un int qui m'indiquera plus tard en quel type je vais convertir fonction atof() par exemple.
data tab; //un tableau qui contient plein de data.
}champ;
 
//et du coup data:
 
typedef struct _DATA{
char Info[100]; // Qui contient une donnée "Marc" par exemple
} data;
Raveen est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2013, 17h26   #4
diogene
Responsable Modération
 
Avatar de diogene
 
Homme Patrick Gonord
Enseignant Chercheur
Inscription : juin 2005
Messages : 5 434
Détails du profil
Informations personnelles :
Nom : Homme Patrick Gonord
Localisation : France, Essonne (Île de France)

Informations professionnelles :
Activité : Enseignant Chercheur
Secteur : Enseignement

Informations forums :
Inscription : juin 2005
Messages : 5 434
Points : 12 952
Points : 12 952
Code :
data tab; //un tableau qui contient plein de data.
Ce n'est pas un tableau, mais une simple structure.
__________________
Publication : Concepts en C

Mon avatar : Glenn Gould

--------------------------------------------------------------------------
Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !
diogene est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2013, 19h21   #5
Raveen
Invité de passage
 
Homme
Étudiant
Inscription : décembre 2012
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Paris (Île de France)

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : décembre 2012
Messages : 4
Points : 0
Points : 0
Problème réglé en partie.

Le problème maintenant viens des boucles d'affichage. Il me faut une condition dans le cas ou a.tables->tab[i].nom (à la ligne 213) soit vide. bah que la boucle s’arrête et pareil pour tout ce qui est des strings de chaque structure.

Code mis à jour:

Code :
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TYPE char;
 
void *myalloc(size_t n){
void *ret = malloc(n);
if(ret == NULL){
printf("Erreur de Malloc");
exit(1);
}
return ret;
}
 
void *my_realloc(void *p, size_t size) {
  p = realloc(p, size);
  if (p == NULL) {
    fprintf (stderr, "Erreur : impossible de reallouer la mémoire\n");
    exit(-1);
  }
  return p;
}
 
 
 
/*-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------*/
 
 
typedef struct _vector{
char *tab;
int n;
int cap;
} *vector;
 
typedef struct _DATA{
char *Info;
} *data;
 
typedef struct _champ{
char entete[100]; //max
int type_data;
data *tab;
}champ;
 
 
typedef struct _cvector{
champ *tab;
int n;
int cap;
} *cvector;
 
 
typedef struct table{
char nom[100]; //nom de la table
champ tchamp; //des champs
cvector champs;
}table;
 
typedef struct _tvector{
table *tab;
int n;
int cap;
} *tvector;
 
 
typedef struct _database{
char nom[200];
table d;
tvector tables;
}database;
 
/*-----------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------*/
 
 
tvector new_tvector(){
tvector p= myalloc(sizeof(*p));
p->tab= myalloc(25 * sizeof(table));
p->cap=25;
p->n = 0;
return p;
}
 
cvector new_cvector(){
cvector r= myalloc(sizeof(*r));
r->tab= myalloc(25 * sizeof(champ));
r->cap=25;
r->n = 0;
return r;
}
 
data new_data(){
data tab=myalloc(50*sizeof(data));
tab->Info= myalloc(150*sizeof(char));
return tab;
}
 
champ new_champs(char *cname, int typ){
champ nouv;
strcpy(nouv.entete,cname);
nouv.type_data= typ;
nouv.tab = myalloc(50*sizeof(data));
return nouv;
}
 
table new_table(char *tname/*, char *cnom */){
table nouv ;
strcpy(nouv.nom,tname);
//nouv.tchamp = new_champs(cnom, 1);
nouv.champs = new_cvector();
return nouv;
}
 
database new_database(char *dname/* ,char *tnom, char *cnom*/){
database nouv;
strcpy(nouv.nom,dname);
//nouv.dtable=new_table(tnom, cnom);
nouv.tables = new_tvector();
return nouv;
}
 
 
 
 
 
 
/* LES FONCTIONS RESERVE */
 
void reserve(vector v, int cap) {
  if (v->cap == 0) {
    v->tab = myalloc (cap *sizeof (*v->tab));
    v->cap = cap;
    return;
  }
  v->tab = my_realloc(v->tab, cap *sizeof (*v->tab));
  v->cap = cap;
  if (v->n > cap)
    v->n=cap;
}
 
 
void treserve(tvector v, int cap) {
  if (v->cap == 0) {
    v->tab = myalloc (cap *sizeof (*v->tab));
    v->cap = cap;
    return;
  }
  v->tab = my_realloc(v->tab, cap *sizeof (*v->tab));
  v->cap = cap;
  if (v->n > cap)
    v->n=cap;
}
 
void creserve(cvector v, int cap){
    if (v->cap == 0) {
    v->tab = myalloc (cap *sizeof (*v->tab));
    v->cap = cap;
    return;
  }
  v->tab = my_realloc(v->tab, cap *sizeof (*v->tab));
  v->cap = cap;
  if (v->n > cap)
    v->n=cap;
}
/*LES FONCTIONS PUSHBACK*/
 
void push_back(vector v, char DATA){
  if (v->cap == v->n) {
    if (v->cap == 0)
      reserve (v,1);
    else
      reserve (v, 2 * v->cap);
  }
  v->tab[v->n] = DATA;
  v->n++;
}
 
void tpush_back(tvector a, table b){
  if(a->cap == a->n) {
    if (a->cap == 0)
       treserve (a, 1);
    else
       treserve (a, (2 * a->cap));
  }
  a->tab[a->n] = b;
  a->n++;
  }
 
void cpush_back(cvector a, champ t){
if(a->cap == a->n) {
    if (a->cap == 0)
       creserve (a, 1);
    else
       creserve (a, (2 * a->cap));
  }
  a->tab[a->n] = t;
  a->n++;
  }
 
int sizetable(database a){
 
        return a.tables->tab->champs->n;
    }
 
 
void affiche(database a){
     int i,j,l;
     printf("%s \n", a.nom);
 
     for(i=0;i<2;i++){
       // if(a.tables->tab[i].nom != )
    printf("\n\n%s \n",a.tables->tab[i].nom);
   else{printf("Erreur: Ne contient pas d'autres tables.");
    break;}
    for(j=0;j<3;j++){
    printf("\n %s ",a.tables->tab[i].champs->tab[j].entete);
 
     for(l=0;l<4;l++){
     printf("%s ",a.tables->tab[i].champs->tab[j].tab[l]->Info);}
 
}}printf(".");}
 
database init(){
    int i;
    database a = new_database("DataEtudiant");
    table un=new_table("Etudiants");
    a.tables->tab[0]=un;
    champ id=new_champs("ID",0);
    un.champs->tab[0] = id;
    for(i=0;i<4;i++)  id.tab[i]=new_data();
    id.tab[0]->Info= "1";
    id.tab[1]->Info= "2";
    id.tab[2]->Info= "3";
    id.tab[3]->Info= "4";
    champ nom=new_champs("Nom",1);
    un.champs->tab[1] = nom;
    for(i=0;i<4;i++)  nom.tab[i]=new_data();
    nom.tab[0]->Info= "VLAD";
    nom.tab[1]->Info= "RAVEEN";
    nom.tab[2]->Info= "CIOBI";
    nom.tab[3]->Info= "SARKO";
    champ prenom= new_champs("Prenom",1);
    un.champs->tab[2] = prenom;
    for(i=0;i<4;i++)  prenom.tab[i]=new_data();
    prenom.tab[0]->Info= "JEAN";
    prenom.tab[1]->Info= "PERERA";
    prenom.tab[2]->Info= "STEFAN";
    prenom.tab[3]->Info= "ZY";
 
    table deux = new_table("Notes");
    a.tables->tab[1] = deux;
    return a;
}
int main(){
    database a = init();
    affiche(a);
    return 0;
}
Raveen est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/01/2013, 15h35   #6
Raveen
Invité de passage
 
Homme
Étudiant
Inscription : décembre 2012
Messages : 4
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Paris (Île de France)

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : décembre 2012
Messages : 4
Points : 0
Points : 0
a.tables->tab[i].nom != NULL

pour la condition devrait-elle marcher?
Raveen est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/01/2013, 20h36   #7
Neckara
Rédacteur
 
Avatar de Neckara
 
Homme Denis
Étudiant
Inscription : décembre 2011
Messages : 2 575
Détails du profil
Informations personnelles :
Nom : Homme Denis
Localisation : France, Loire (Rhône Alpes)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : décembre 2011
Messages : 2 575
Points : 6 984
Points : 6 984
Envoyer un message via MSN à Neckara Envoyer un message via Skype™ à Neckara
Citation:
Envoyé par Raveen Voir le message
a.tables->tab[i].nom != NULL

pour la condition devrait-elle marcher?
Cette condition sera toujours vrai, en effet, nom est un tableau qui sera implicitement convertit en un pointeur sur le premier élément du tableau qui existera toujours.

Si tu veux savoir si nom n'est pas "vide", tu peux soit regarder si nom[0] != '\0' ou si strlen(nom) != 0.
Par contre il ne faudra pas oublier d'initialiser ton tableau.
__________________
Recherche devs C++ motivés et sérieux pour Last Dungeon.

Chaîne Youtube : Vidéos

Ma page DVP : http://neckara.developpez.com/
Neckara est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 11h19.


 
 
 
 
Partenaires

Hébergement Web