[2.2👍] Chargement des données OK

Par contre il y a une fuite mémoire, oopsie :D
This commit is contained in:
Theo 2024-09-07 20:57:13 +02:00
parent 78f47c5d91
commit 937d941c27
2 changed files with 151 additions and 5 deletions

149
main.c
View File

@ -1,7 +1,27 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
void afficher(void);
int initWindow(int, char**);
void destroyWindow(int);
long getFileSize(FILE*);
void parseFile(char*, long);
bool charger(char*);
void printCourbeTerminal(void);
// Variables donneés
#define MAXC 20
#define MAXV 100
float courbe[MAXC][MAXV];
float valmin[MAXC];
float valmax[MAXC];
int nc = 0;
void afficher(void)
{
glMatrixMode(GL_PROJECTION);
@ -19,19 +39,138 @@ void afficher(void)
glFlush();
}
int main(int argc, char* argv[])
int initWindow(int argc, char* argv[])
{
int vx1 = 0, vx2 = 400, vy1 = 0, vy2 = 400;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowPosition(vx1, vy1);
glutInitWindowSize(vx2, vy2);
int win = glutCreateWindow("glut");
int window = glutCreateWindow("TP1");
glutDisplayFunc(afficher);
glutMainLoop();
return window;
}
glutDestroyWindow(win);
void destroyWindow(int window)
{
glutDestroyWindow(window);
}
long getFileSize(FILE* file)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
rewind(file);
return fileSize;
}
void parseFile(char* file, long size)
{
nc = atoi(file);
printf("nc=%d\n", nc);
printf("====\n%s\n====\n", file);
// Pointeur mobile dans le fichier
char* pointeur = file;
unsigned int line = 0;
while(line < nc+1)
{
/* Cherche le premier \n à partir du pointeur
* /!\ on suppose un fichier encodé en UNIX (LF) /!\ */
pointeur = strchr(pointeur, '\n') + 1;
// printf(" ->\"%c\"", *pointeur);
int taille_tableau = atoi(pointeur);
// printf(" ->\"%d\" \n", taille_tableau);
// On suppose que les nombres sont séparés par un espace
for(unsigned int i = 0; i < taille_tableau; i++)
{
pointeur = strchr(pointeur, ' ') + 1;
float num = strtof(pointeur, 0);
courbe[line][i] = num;
if(i == 0)
{
valmin[line] = num;
valmax[line] = num;
}else{
if(num < valmin[line])
valmin[line] = num;
if(num > valmax[line])
valmax[line] = num;
}
}
line++;
}
}
bool charger(char* nom)
{
FILE* file = fopen(nom, "r");
if(file == NULL)
{
printf("Erreur ouverture fichier.\n");
return false;
}
long fileSize = getFileSize(file);
char* buffer = (char*)malloc(sizeof(char) * fileSize);
if(buffer == NULL)
{
printf("Erreur allocation mémoire.\n");
return false;
}
int res = fread(buffer, sizeof(char), fileSize, file);
if(res == 0)
{
printf("Erreur lecture fichier.\n");
return false;
}
fclose(file);
parseFile(buffer, fileSize);
free(buffer);
return true;
}
void printCourbeTerminal(void)
{
for(unsigned int i = 0; i < MAXC; i++)
{
for(unsigned int j = 0; j < MAXV; j++)
{
printf("%f", courbe[i][j]);
if(j < MAXV-1)
printf(", ");
}
printf("\n");
}
}
int main(int argc, char* argv[])
{
//int window = initWindow(argc, argv);
//glutMainLoop();
if(!charger("valeurs.txt"))
return EXIT_FAILURE;
//printCourbeTerminal();
//destroyWindow(window);
return EXIT_SUCCESS;
}

7
valeurs.txt Normal file
View File

@ -0,0 +1,7 @@
5
5 1 2 3 4 5
6 21 543.3 75 34 76 89
1 234
4 123 582.123 723 612
3 238 57 92.2973
6 2398 238 89172.23 748 1873 897