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

64 lines
1.6 KiB
C
Raw Normal View History

2022-06-01 12:32:04 +02:00
#include "getArray.h"
#include "initialParameters.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);
}
}
}
void print(int** p, int N, int M) {
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 17:01:23 +02:00
int **getRawDataArray(){
2022-06-01 12:32:04 +02:00
int **p;
p = get(nRow, nCol);
2022-06-01 17:01:23 +02:00
fillRawData(p ,nRow, nCol);
2022-06-01 12:32:04 +02:00
//print(p, nRow, nCol);
2022-06-01 17:01:23 +02:00
clearRawData(nRow);
//freeArray(p ,nRow);
2022-06-01 12:32:04 +02:00
return 0;
2022-06-01 17:01:23 +02:00
}
void main(int argc , char** argv){
getRawDataArray();
2022-06-01 12:32:04 +02:00
}