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)

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::ostreamoperator<<(std::ostreamos, const hexPackpack)
    {
        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(formPreffp);
bool savePreferences(formPreffp);

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(formPreffp)
{
    
std::stringstream k;

    
std::ifstream hfile;
    
hfile.open(szPrefFilestd::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 intbuff[0] << 24 buff[1] << 16 buff[2] << buff[3];
    }

    if (
hfile >> buff[0] >> buff[1] >> buff[2] >> buff[3])
    {
        
fp.left = (unsigned intbuff[0] << 24 buff[1] << 16 buff[2] << buff[3];
    }

    if (
hfile >> buff[0] >> buff[1] >> buff[2] >> buff[3])
    {
        
fp.indexDevice = (unsigned intbuff[0] << 24 buff[1] << 16 buff[2] << buff[3];
    }

    if (
hfile >> buff[0] >> buff[1] >> buff[2] >> buff[3])
    {
        
fp.indexSamples = (unsigned intbuff[0] << 24 buff[1] << 16 buff[2] << buff[3];
    }

    
hfile.close();
    return 
true;
}

bool savePreferences(formPreffp)
{
    
std::stringstream k;

    
std::ofstream hfile;
    
hfile.open(szPrefFilestd::ios::out|std::ios::binary);
    if(
hfile.bad())
        return 
false;

    
<< hexPack(fp.top);
    
<< hexPack(fp.left);
    
<< hexPack(fp.indexDevice);
    
<< hexPack(fp.indexSamples);

    
hfile.write(k.str().c_str(), 16);

    
hfile.close();
    return 
true;

Ho scoperto uno strano problema con il seguente codice se gli passo la seguente struttura da salvare:

formPref fp;
fp.top = 10; // Oppure 256
fp.left = 1;
fp.indexDevice = 1;
fp.indexSamples = 1;
al momento della lettura mi verrà tutta sballata: 0,256,256,32654646541

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