Hello,
I recently purchase the TM4C129EXL board. In my application, I need to read a file to set up several variables.
I tried to use the function fopen() but it seems to stuck the code when it's called.
Is there a way to read a file with TM4C129EXL?
I want to use fopen(), fgets(), strtok() and strlen() so I include <stdio.h> and <string.h>.
Here is a part of my code (sorry for the french comments).
void lireFichier(void)
{
UARTprintf("1\n");
FILE * fichier = NULL;
UARTprintf("2\n");
fichier = fopen("tableauxCodeC.txt","r"); // UART print 2 but not 3 nor 4
UARTprintf("3\n");
if (fichier != NULL) //ouverture réussie
{
UARTprintf("4\n");
chaine ligne = {0};
int den = 0, nbLigne = 0;
while ( fgets ( ligne, sizeof ligne, fichier ) != NULL ) //tant qu'on trouve une nouvelle ligne
{
nbLigne++; // on compte les lignes
int i = 0, taille = 1;
strtok(ligne, " "); // strtok "découpe" ligne au niveau des espaces entre les chaînes
while(strtok(NULL, " ") != NULL) taille++; // on compte le nombre de chaines par ligne
if(nbLigne < 21) // partie des transformées en Z du fichier
{
if( den == 0) //on a un numérateur
{
int idata = (int)((nbLigne-1)/2);
data[idata].dimNum = taille;
int compteur = 0; // compteur de chaines
while( i <= (sizeof ligne) && ligne[i])
{
data[idata].num[compteur]=atof(ligne+i); // transforme la chaine en float et l'enregistre à l'adresse du pointeur
i += strlen(ligne+i) + 1; //chaine suivante
compteur += 1; //float suivant
}
den = 1;
}
else //on a un dénominateur
{
int idata = (int)((nbLigne-2)/2);
data[idata].dimDen = taille;
int compteur = 0; // compteur de chaines
while( i <= (sizeof ligne) && ligne[i])
{
data[idata].den[compteur]=atof(ligne+i); // transforme la chaine en float et l'enregistre à l'adresse du pointeur
i += strlen(ligne+i) + 1; //chaine suivante
compteur += 1; //float suivant
}
den = 0;
}
}
else //polynome
{
pFF.dim = taille;
int compteur = 0; // compteur de chaines
while( i <= (sizeof ligne) && ligne[i])
{
pFF.coeff[compteur] = atof(ligne+i); // transforme la chaine en float et l'enregistre à l'adresse du pointeur
i += strlen(ligne+i) + 1; //chaine suivante
compteur += 1; //float suivant
}
}
}
fclose(fichier);
}
}
Here is the kind of structure I have in my file:
1.1 2
1 2 3.2
4 5 6.2
7 8 9.2e-5
1 2
1 2 3
4 5 6
7 8 9
1 2
1.001 2 3 10
It is always 10 lines of real numbers. There is 10 number per line max, I don't know how many number there is (it may vary).
These datas are generated with Matlab.
If there is no way to read a *.txt file, what do you suggest? Basicaly I need to set up some variables with datas that I generate with my PC and transmit it to the TM4C129EXL.
I am considering using UART, but this code works with a console application with code::blocks
If you have any questions please let me know.
Thank you.