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
|
// structures dans fichier variables.h
struct NODE {
double x;
double y;
double z;
};
struct DATA {
struct NODE *pnodes;
struct WIDGET pwidgets;
int dime;
int Nnodes;
int Nelems;
};
// connection signal du clic dans le main.c
struct DATA pdata;
pdata.pnodes = NULL;
pdata.dime = 2;
pdata.Nnodes = 0;
g_signal_connect(G_OBJECT(pdata.pwidgets.m_ouvrir), "activate", G_CALLBACK(callback_ouvrir_clic), &pdata);
// fonction qui gère l'évènement du clic
void callback_ouvrir_clic(GtkWidget *w, gpointer user_data) {
struct DATA *pdata = (struct DATA*)user_data;
pdata->pnodes = ouvrir_fichier_points(&pdata->dime, &pdata->Nnodes);
}
// fonction pour lire le fichier dans structure NODE
struct NODE* ouvrir_fichier_points(int *dime, int *Nnodes) {
int Npts = 0;
struct NODE *p = NULL;
FILE *file = fopen("./exemple_maillage/points.txt","r");
if (file == NULL) {
printf("Erreur chargement fichier grille de points .txt");
exit(EXIT_FAILURE);
}
else {
// compter nombre de points (= lignes)
char *line = NULL;
size_t bufsize = 0;
while (getline(&line, &bufsize, file) != -1) Npts++;
free(line);
printf("%d\n", Npts);
// allocation mémoire tableau de noeuds
p = (struct NODE*)malloc(Npts * sizeof(struct NODE));
// retour début du fichier
fseek(file, 0, SEEK_SET);
// remplissage tableau de noeuds
for (int i = 0; i < Npts; i++) {
switch (*dime) {
case 2:
fscanf(file, "%lf %lf", &p[i].x, &p[i].y);
break;
case 3:
fscanf(file, "%lf %lf %lf", &p[i].x, &p[i].y, &p[i].z);
break;
}
}
}
fclose(file);
FILE *fis = fopen("./exemple_maillage/grille.txt","w");
for (int i = 0; i < Npts; i++) {
fprintf(fis, "%lf %lf\n", p[i].x, p[i].y);
}
fclose(fis);
*Nnodes = Npts;
return p;
} |
Partager