Traitement-signal-plantes/Code-C/getArray.c

59 lines
1.5 KiB
C
Raw Normal View History

2022-06-01 12:32:04 +02:00
#include "getArray.h"
#include "fileGestion.h"
int **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
int **array;
array = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
array[i] = malloc( M*sizeof(int) );
return array;
}
2022-06-01 17:01:23 +02:00
void fillRawData(int** p, int N, int M) {
2022-06-01 12:32:04 +02:00
int i, j;
char buffer[200];
char *token;
2022-06-01 17:01:23 +02:00
FILE *f = fopen("rawData.txt","r");
2022-06-01 12:32:04 +02:00
for(i = 0 ; i < N ; i++){
2022-06-01 17:01:23 +02:00
if (!fgets(buffer, sizeof buffer, f)) break; // condition d'arret de la boucle si fichier fini
2022-06-01 12:32:04 +02:00
//printf(buffer);
token = strtok(buffer, ","); // séparation valeur par virgule initiale : csv
p[i][0] = atoi(token);
for(j = 1 ; j < M ; j++){
token = strtok(NULL, ","); // séparation valeur par virgule : csv
if(token == NULL) break; // condition d'arrêt de la séparartion
p[i][j] = atoi(token); //transtypage char to int
//printf(token);
}
}
}
2022-06-01 18:02:32 +02:00
void printArray(int** p, int N, int M) {
2022-06-01 12:32:04 +02:00
int i, j;
for(i = 0 ; i < N ; i++)
for(j = 0 ; j < M ; j++)
printf("array[%d][%d] = %d\n", i, j, p[i][j]);
}
void freeArray(int** p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
2022-06-01 18:02:32 +02:00
int **getRawDataArray(int N , int M){
2022-06-01 12:32:04 +02:00
int **p;
2022-06-01 18:02:32 +02:00
p = get(N, M);
fillRawData(p ,N, M);
//printArray(p, nRow, nCol);
clearRawData(N);
2022-06-01 17:01:23 +02:00
//freeArray(p ,nRow);
2022-06-01 18:02:32 +02:00
return p;
2022-06-01 12:32:04 +02:00
}