Part Number: TDA4VM
Tool/software:
Hello TI,
My goal is to write C++ code that allows me to load a binary file in CCS and then store those values in an array.
My array contains complex numbers, having floating real and imaginary parts. This type has been implemented within a class that I called "CGestionFichier," which is used to manage the downloading and creation of files within my project. Below, here is the code for the header file as well as the source file of this class:
/*
* CGestionFichier.hpp
*
* Created on: 15 avr. 2025
*/
#ifndef CGESTIONFICHIER_HPP_
#define CGESTIONFICHIER_HPP_
#include "CFiltreFir.hpp"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class CGestionFichier{
public :
/* --- Constructeur par defaut --- */
CGestionFichier();
/* --- Destructeur par defaut --- */
virtual ~CGestionFichier();
/*--- CHARGEMENT FICHIER CONTENANT DES ECHANTILLONS COMPLEXES DE x(t) --- */
/*
* @p_nomFichier : nom du fichier a charger en mem
* @return : pointeur
*/
CFiltreFir::SComplexe* chargerFichierEchantillonEnMemoire(const string& p_nomFichier);
/* --- CHARGEMENT FICHIER CONTENANT DES COEFFICIENTS DU FILTRE --- */
// float* chargerFichierCoefficientsEnMemoire(const std::string& p_nomFichier);
// pour moi la version la plus coherent est celle renvoyant du int
int* chargerFichierCoefficientsEnMemoire(const string& p_nomFichier);
/* --- SAUVEGARDE DANS UN FICHIER DES ECHANTILLONS FILTRES --- */
/*
* @p_nomFichier nom du fichier de sauvegarde pour les eschantillons
* @p_echantillons echantillons a sauvegarder
* @p_nbEchantillons Nombre d echantillons a sauvegarder
*
*/
bool sauvegarderEchantillonsDansFichier(const string& p_nomFichier, CFiltreFir::SComplexe* p_echantillons, size_t p_nbEchantillons);
/* --- Calcul le nbr d octets du fichier charge --- */
// static const unsigned int compterNombreOctets(const std::string& p_nomFichier);
private :
/* --- Calcul le nbr d octets du fichier charge --- */
static const unsigned int compterNombreOctets(const string& p_nomFichier);
};
#endif /* CGESTIONFICHIER_HPP_ */ /*
* CGestionFichier.cpp
*
* Created on: 15 avr. 2025
*/
#include "CFiltreFir.hpp"
#include "CGestionFichier.hpp"
#include <string>
#include <fstream>
#include <iostream>
#include <ios> //add pour corriger pb Binf
using namespace std;
/* --- Constructeur par defaut --- */
CGestionFichier::CGestionFichier(){};
/* --- Destructeur par defaut --- */
CGestionFichier::~CGestionFichier(){};
/* CALCUL DU NBR D OCTETS DU FICHIER CHARGE */
const unsigned int CGestionFichier::compterNombreOctets(const string& p_nomFichier){
/* Chargement des donnees en memoire */
ifstream fichierTaille(p_nomFichier, ios::binary);
/* ECHEC D OUVERTURE DU FICHIER */
if (!fichierTaille.is_open()) {
cout << "Fichier introuvable." << endl;
return 0;
}
/* PLACEMENT DU POINTEUR A LA FIN DU FICHIER */
fichierTaille.seekg(0, ios::end);
/* RECUPERATION POISTION DU POINTEUR */
/* DONNE NBR D OCTETS */
const unsigned int nbOctets = fichierTaille.tellg();
/* FERMETURE DU FICHIER */
fichierTaille.close();
/* CAS FICHIER VIDE */
if (nbOctets == 0) {
cout << "Aucune donnee dans le fichier." << endl;
return 0;
}
return nbOctets;
}
/*--- CHARGEMENT FICHIER CONTENANT DES ECHANTILLONS COMPLEXES DE x(t) --- */
CFiltreFir::SComplexe* CGestionFichier::chargerFichierEchantillonEnMemoire(const string& p_nomFichier){
/* Nombre d octets du fichier p_nomFichier */
const unsigned int nbOctets = CGestionFichier::compterNombreOctets(p_nomFichier);
/* Allocation memoire pour stocker les data via malloc */
CFiltreFir::SComplexe* tableauEchantillons = (CFiltreFir::SComplexe*)malloc(nbOctets * sizeof(CFiltreFir::SComplexe));
/* Verification de l allocation */
if (tableauEchantillons == nullptr) {
cout << "Erreur d'allocation memoire." << endl;
return nullptr;
}
/* Chargement des donnees en memoire */
// ifstream permet de lire les lignes du fichier
// type d'objet : std::ifstream = reprensete un flux d entree pour lire des fichiers
// fichier : c est le nom de l objet creee pour manip le flux de lecture
// std :: ios :: binary = permet d indiquer le mode d ouverture du fichier => ici on demande a l ouvrir le fichier en mode binaire
// note : si mode d ouverure non precise, par defaut on ouvre le fichier en mode texte
ifstream fichier(p_nomFichier, ios::binary);
/* SUCCESS D OUVERTURE DU FICHIER */
if(fichier.is_open()){
//la fonction suivante permet de recuperer les echantillons
//1er argument de la fonction : specifier le pointeur vers l endroit ou on souhaite stocker les data
//2eme argument : specifie le nombre d octets a lire
// fichier.read(reinterpret_cast<char*>(tableauEchantillons), nbOctets); //ligne generant une Binf
// MODIFIE
if(fichier.read(reinterpret_cast<char*>(tableauEchantillons), nbOctets)){
cout << "La fin du fichier n'a pas été atteinte" << endl;
}
/* Verification de l etat du fichier ouvert */
/* Cas 1 : Le fichier est corrompu ou incomplet */
if (fichier.gcount() != static_cast<std::streamsize>(nbOctets)) {
cout << "Erreur : Fichier incomplet ou corrompu" << endl;
free(tableauEchantillons); //liberation de memoire allouee dynamiquement par ex via malloc, ...
// interet d utiliser free : il est indisp de liberer mem allouee via malloc quand plus necess pour eviter des fuites mem
return nullptr;
}
/* Cas 2 : Le fichier ouvert est bien complet */
fichier.close();
cout << "Fichier charge en memoire avec succes." << endl;
}
/* ECHEC D OUVERTURE DU FICHIER */
else{
cout << "Erreur : impossible d'ouvrir le fichier." << endl;
free(tableauEchantillons);
return nullptr;
}
return tableauEchantillons;
}
/* --- CHARGEMENT FICHIER CONTENANT DES COEFFICIENTS DU FILTRE --- */
int* CGestionFichier::chargerFichierCoefficientsEnMemoire(const string& p_nomFichier){
/* Nombre d octets du fichier p_nomFichier */
const unsigned int nbOctets = compterNombreOctets(p_nomFichier);
/* Allocation memoire pour stocker les data via malloc */
// float* tableauEchantillons = (float*)malloc(nbOctets * sizeof(float));
// pour moi version coherente est ci dessous
int* tableauEchantillons = (int*)malloc(nbOctets * sizeof(int));
/* Verification de l allocation */
if (tableauEchantillons == nullptr) {
cout << "Erreur d'allocation memoire." << endl;
return nullptr;
}
/* Chargement des donnees en memoire */
ifstream fichier(p_nomFichier, ios::binary);
/* SUCCESS D OUVERTURE DU FICHIER */
if (fichier.is_open()) {
fichier.read(reinterpret_cast<char*>(tableauEchantillons), nbOctets);
/* Verification de l etat du fichier ouvert */
/* Cas 1 : Le fichier est corrompu ou incomplet */
if (fichier.gcount() != static_cast<std::streamsize>(nbOctets)) {
cout << "Erreur : Fichier incomplet ou corrompu" << endl;
free(tableauEchantillons);
return nullptr;
}
/* Cas 2 : Le fichier ouvert est bien complet */
fichier.close();
cout << "Fichier charge en memoire avec succes." << endl;
}
else {
/* ECHEC D OUVERTURE DU FICHIER */
cout << "Erreur : impossible d'ouvrir le fichier." << endl;
free(tableauEchantillons);
return nullptr;
}
return tableauEchantillons;
}
/* --- SAUVEGARDE DANS UN FICHIER DES ECHANTILLONS FILTRES --- */
bool CGestionFichier::sauvegarderEchantillonsDansFichier(const string& p_nomFichier, CFiltreFir::SComplexe* p_echantillons, size_t p_nbEchantillons){
// Permet d ouvrir un fichier et ecrire directement dans le fichier a partiri du programme au nom de fichier
// Mode d ouverture du fichier : en mode binaire
ofstream fichier(p_nomFichier, ios::binary);
/* ECHEC D OUVERTURE DU FICHIER */
if (!fichier.is_open()) {
cout << "Erreur : impossible d'ouvrir le fichier pour ecriture." << endl;
return false;
}
/* SUCCES D OUVERTURE DU FICHIER */
/* ECRITURE DES ECHANTILLONS DANS FICHIER */
fichier.write(reinterpret_cast<const char*>(p_echantillons), p_nbEchantillons * sizeof(CFiltreFir::SComplexe));
/* CAS D ERREUR AU MOMENT DE L ECRITURE DANS FICHIER */
if (!fichier) {
cout << "Erreur lors de l'ecriture des données." << endl;
fichier.close();
return false;
}
/* CAS SUCCES D ECRITURE DANS FICHIER */
fichier.close();
cout << "Echantillons savegardes dans : " << p_nomFichier << endl;
return true;
}In my case, I only use the functions and given that the main function of my project is as follows:
#include "CFiltreFir.hpp"
#include "CGestionFichier.hpp"
#include <string>
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
/* --- INIT PARAM ENTREE DU FILTRE --- */
/* Definition du nbr de symboles */
long nbSymboles = 131704;
/* CHARGEMENT DES ECHANTILLONS DE x(t)*/
// Rappel : les echantillons du signal d entree x(t) sont complexes
// Fonction realisee par la methode chargerFichierEchantillonEnMemoire de la classe CGestionFichier
/* RAPPEL FORMAT FONCTION : CFiltreFir::SComplexe* chargerFichierEchantillonEnMemoire(const std::string& p_nomFichier) */
CGestionFichier getFichier;
// Chemin d acces au fichier .bin
// Pointeur vers une cdc
string cheminSignalEnt = "T:/depots/stage_jacinto7/FiltreFIR_TROPO/pattern/signalFiltreIn.bin";
// Chargement des donnees dans le fichier getEchant
CFiltreFir::SComplexe* signalEnt = getFichier.chargerFichierEchantillonEnMemoire(cheminSignalEnt);
return 0;
}
The problem encountered with this project is as follows: when I start a debug session on the DSP C7x, I find myself in an infinite loop. If I pause this session, I consistently land in the code of , more precisely 
Now, if I debug step by step, I find that the problem arises from the use of the function (line 86 of the file ) in . As soon as I reach this line, the code enters an infinite loop.
What could this be due to? Can anyone shed light on the problem encountered?
Thank you in advance,
Mélanie



