Salve,
ho uno strano problema con il seguente codice il cui scopo e di salvare degli integer in un file. Ogni integer (4 bytes) viene salvato/letto in una rappresentazione binaria big-endian: (per adesso mi accontento di salvare una semplice struttura di 4 integer (16 bytes su file)
Ho scoperto uno strano problema con il seguente codice se gli passo la seguente struttura da salvare:Codice PHP:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <fstream>
#include <tchar.h>
#include <cstdlib>
const char * szPrefFile = _T("C:\\Users\\giangaetano\\Desktop\\mmio\\hexpack.txt");
class hexPack
{
public:
hexPack(int value) : m_value(value) { }
friend std::ostream& operator<<(std::ostream& os, const hexPack& pack)
{
return os.put((pack.m_value >> 24) & 0xFF).put((pack.m_value >> 16) & 0xFF).put((pack.m_value >> 8) & 0xFF).put(pack.m_value & 0xFF);
}
private:
unsigned m_value;
};
// Struct Preferences //
//
struct formPref
{
unsigned int top; // Seek 0 (4 Bytes)
unsigned int left; // Seek 4 (4 Bytes)
unsigned int indexDevice; // Seek 8 (4 Bytes)
unsigned int indexSamples; // Seek 12 (4 Bytes)
};
////////////////////////
bool loadPreferences(formPref& fp);
bool savePreferences(formPref& fp);
int _tmain()
{
{
// Fill Struct
//
formPref fp;
fp.top = 1;
fp.left = 1;
fp.indexDevice = 1;
fp.indexSamples = 1;
if(!savePreferences(fp))
std::cout << "Error saving!" << std::endl;
}
{
// Get Struct Filled
//
formPref fp;
if(!loadPreferences(fp))
std::cout << "Error loading!" << std::endl;
std::cout << fp.top << std::endl;
std::cout << fp.left << std::endl;
std::cout << fp.indexDevice << std::endl;
std::cout << fp.indexSamples << std::endl;
}
system("pause");
return EXIT_SUCCESS;
}
bool loadPreferences(formPref& fp)
{
std::stringstream k;
std::ifstream hfile;
hfile.open(szPrefFile, std::ios::in|std::ios::binary);
if(hfile.bad())
return false;
unsigned char buff[4] = {0};
if (hfile >> buff[0] >> buff[1] >> buff[2] >> buff[3])
{
fp.top = (unsigned int) buff[0] << 24 | buff[1] << 16 | buff[2] << 8 | buff[3];
}
if (hfile >> buff[0] >> buff[1] >> buff[2] >> buff[3])
{
fp.left = (unsigned int) buff[0] << 24 | buff[1] << 16 | buff[2] << 8 | buff[3];
}
if (hfile >> buff[0] >> buff[1] >> buff[2] >> buff[3])
{
fp.indexDevice = (unsigned int) buff[0] << 24 | buff[1] << 16 | buff[2] << 8 | buff[3];
}
if (hfile >> buff[0] >> buff[1] >> buff[2] >> buff[3])
{
fp.indexSamples = (unsigned int) buff[0] << 24 | buff[1] << 16 | buff[2] << 8 | buff[3];
}
hfile.close();
return true;
}
bool savePreferences(formPref& fp)
{
std::stringstream k;
std::ofstream hfile;
hfile.open(szPrefFile, std::ios::out|std::ios::binary);
if(hfile.bad())
return false;
k << hexPack(fp.top);
k << hexPack(fp.left);
k << hexPack(fp.indexDevice);
k << hexPack(fp.indexSamples);
hfile.write(k.str().c_str(), 16);
hfile.close();
return true;
}
al momento della lettura mi verrà tutta sballata: 0,256,256,32654646541formPref fp;
fp.top = 10; // Oppure 256
fp.left = 1;
fp.indexDevice = 1;
fp.indexSamples = 1;
al contrario se in fp.top metto valori più alti di 25 tutto va bene, in pratica in qualsiasi campo della struttura se metto un valore 10 o anche di poco maggiore in fase di lettura mi sballerà tutta la struttura...mah!
com'è possibile?
grazie