Problème de cast à la lecture de short int dans un fichier bin
Bonjour,
J'ai fais un code très simple pour manipuler l'écriture d'un vecteur 2D(6,4) dans un fichier binaire que je relis pour vérifier que le résultat est bien correct.
Mais j'obtiens des erreurs de conversion des nombres : par ex, les chiffres 2 et 3 vont devenir parfois 0. Et à la fin de l'excécution j'ai le message "Erreur de segmentation" alors que j'ai libéré l'espace mémoire de tout les tableaux (non vector)
Est-ce que vous pouvez corriger l'erreur svp. Cette erreur est visible à la lecture du fichier binaire où tab1lecture[0][0]=8608, tab1lecture[1][0]=95, tab1lecture[2][0]=8608 et tab1lecture[3][0]=95.
Code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <iostream>
#include <string>
#include <fstream>
#include <new>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
short nb_y=6;
short nb_x=4;
short nb_x_y=nb_x*nb_y;
short i,j;
string filetestw="testw.bin";
std::vector< std::vector<short> > tab1(nb_x,std::vector<short> (nb_y));
for(i = static_cast<short>(0); i < nb_x; i++)
{
for(j = static_cast<short>(0); j < nb_y; j++) tab1[i][j]=i+2;
}
short* tabtmp1=new short[nb_x_y];
short k;
for(i = static_cast<short>(0); i < nb_x; i++)
{
for(j = static_cast<short>(0); j < nb_y; j++)
{
k=i+(j*nb_x);
// cout << "k=i+(j*nb_x)=" << k << endl;
tabtmp1[k]=tab1[i][j];
}
}
//----------------------------------------------------------------------------------------
// Vérification du contenu de tabtmp1
for(i = static_cast<short>(0); i < nb_x; i++)
{
for(j = static_cast<short>(0); j < nb_y; j++) cout << "tabtmp1[" << i << "][" << j << "]=" << tabtmp1[i+j*nb_x] << endl;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//Ecriture du vector dans un fichier binaire (avec fct write)
ofstream ficher_sortie(filetestw.c_str(), ios::binary | ios::out | ios::trunc);
if(!ficher_sortie.is_open())
{
cerr << "L'ouverture du fichier de sortie en écriture à echoué\n" << endl;
exit (-1);
}
ficher_sortie.write(reinterpret_cast<const char*>(&tabtmp1),nb_x_y*sizeof(short));
ficher_sortie.close();
delete[] tabtmp1;
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//Lecture du vector dans un fichier binaire (avec fct read)
ifstream ficher_sortie_lu(filetestw.c_str(), ios::binary | ios::in);
if(!ficher_sortie_lu.is_open())
{
cerr << "L'ouverture du fichier de sortie en lecture à echoué\n" << endl;
exit (-1);
}
short * tabtmp1lecture=new short[nb_x_y];
ficher_sortie_lu.read(reinterpret_cast<char*>(&tabtmp1lecture),nb_x_y*sizeof(short));
ficher_sortie_lu.close();
std::vector< std::vector<short> > tab1lecture(nb_x,std::vector<short> (nb_y));
for(i = static_cast<short>(0); i < nb_x; i++)
{
for(j = static_cast<short>(0); j < nb_y; j++) tab1lecture[i][j]=tabtmp1lecture[i+j*nb_x];
}
//----------------------------------------------------------------------------------------
// Vérification du contenu de tab1lecture
for(i = static_cast<short>(0); i < nb_x; i++)
{
for(j = static_cast<short>(0); j < nb_y; j++) cout << "tabtmp1lecture[" << i << "][" << j << "]=" << tabtmp1lecture[i+j*nb_x] << endl;
}
//----------------------------------------------------------------------------------------
delete[] tabtmp1lecture;
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// Vérification du contenu de tab1lecture
// for(i = static_cast<short>(0); i < nb_x; i++)
// {
// for(j = static_cast<short>(0); j < nb_y; j++) cout << "tab1lecture[" << i << "][" << j << "]=" << tab1lecture[i][j] << endl;
// }
//----------------------------------------------------------------------------------------
return (0);
} |
Resultat :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
tabtmp1[0][0]=2
tabtmp1[0][1]=2
tabtmp1[0][2]=2
tabtmp1[0][3]=2
tabtmp1[0][4]=2
tabtmp1[0][5]=2
tabtmp1[1][0]=3
tabtmp1[1][1]=3
tabtmp1[1][2]=3
tabtmp1[1][3]=3
tabtmp1[1][4]=3
tabtmp1[1][5]=3
tabtmp1[2][0]=4
tabtmp1[2][1]=4
tabtmp1[2][2]=4
tabtmp1[2][3]=4
tabtmp1[2][4]=4
tabtmp1[2][5]=4
tabtmp1[3][0]=5
tabtmp1[3][1]=5
tabtmp1[3][2]=5
tabtmp1[3][3]=5
tabtmp1[3][4]=5
tabtmp1[3][5]=5
tabtmp1lecture[0][0]=8608
tabtmp1lecture[0][1]=2
tabtmp1lecture[0][2]=2
tabtmp1lecture[0][3]=2
tabtmp1lecture[0][4]=2
tabtmp1lecture[0][5]=2
tabtmp1lecture[1][0]=95
tabtmp1lecture[1][1]=3
tabtmp1lecture[1][2]=3
tabtmp1lecture[1][3]=3
tabtmp1lecture[1][4]=3
tabtmp1lecture[1][5]=3
tabtmp1lecture[2][0]=8608
tabtmp1lecture[2][1]=4
tabtmp1lecture[2][2]=4
tabtmp1lecture[2][3]=4
tabtmp1lecture[2][4]=4
tabtmp1lecture[2][5]=4
tabtmp1lecture[3][0]=95
tabtmp1lecture[3][1]=5
tabtmp1lecture[3][2]=5
tabtmp1lecture[3][3]=5
tabtmp1lecture[3][4]=5
tabtmp1lecture[3][5]=5
Erreur de segmentation |