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

49 lines
1.5 KiB
C

#include "include/fileGestion.h"
/**
* @brief function that delete nRow lign in the beginning of the file rawData.csv . This function is necessary to not deal with the same ligns over and over
*
* @param nRow number of lign in the beginning of the file rawData.csv that has to be delete
*/
void clearRawData(int nRow)
{
char buffer[256];
FILE *f = fopen("newFile.csv", "w+");
FILE *g = fopen("rawData.csv", "r");
for (int i = 0; i < nRow; i++)
{ // first the program read the first nRow ligns of the csv file but do nothing
fgets(buffer, sizeof buffer, g);
}
while (1)
{ // then, till the end of the csv file it copy the lign to a new csv : newFile.csv
if (!fgets(buffer, sizeof buffer, g))
break;
fprintf(f, "%s", buffer);
}
remove("rawData.csv");
rename("newFile.csv", "rawData.csv"); // finally we remove the original file and rename the new one to replace rawData.csv
fclose(f);
fclose(g);
}
/**
* @brief use to write one lign in the file "fileName"
*
* @param array array that contaign all the values to write in the file
* @param nCol size of the array (correspond to the number of captor used)
*/
void appendDataInFile(char *fileName, double array[], int nCol)
{
FILE *f = fopen(fileName, "a+");
for (int i = 0; i < nCol; i++)
{
if (i < nCol - 1)
{
fprintf(f, "%f , ", array[i]);
}
else
{
fprintf(f, "%f\n", array[i]);
}
}
fclose(f);
}