StructToCharArray

Copia struttura POD nell'array di tipo uchar.

bool  StructToCharArray(
   const void&  struct_object,     // struttura
   uchar&       char_array[],      // array
   uint         start_pos=0        // posizione iniziale nell'array
   );

Parametri

struct_object

[in] Riferimento a qualsiasi tipo di Struttura POD (contenente solo tipi di dati semplici).

char_array[]

[in]  array di tipo uchar.

start_pos=0

[in] Posizione nell'array a partire dalla quale vengono aggiunti i dati copiati.

Valore di ritorno

Restituisce true se avvenuto con successo, altrimenti false.

Note

Durante la copia, l'array dinamico si espande automaticamente (ArrayResize) se non c'è abbastanza spazio. Se l'array non può essere espanso fino al valore richiesto, la funzione restituisce un errore.

 

Esempio:

uchar    ExtCharArray[];
MqlRates ExtRates[1];
 
//+--------------------------------------------------------------------------------+
//| Script program start function                                                  |
//+--------------------------------------------------------------------------------+
void OnStart()
  {
//--- copia i dati di una barra corrente nella struttura MqlRates
   if(CopyRates(Symbol(), PERIOD_CURRENT01ExtRates)!=1)
     {
      Print("CopyRates() failed. Error code: "GetLastError());
      return;
     }
 
//--- stampa i dati ricevuti nella struttura MqlRates nel journal
   Print("Data in the structure immediately after receiving it:");
   MqlRatesPrint(ExtRates[0]);
 
//--- copia la struttura nell'array di tipo uchar
   ResetLastError();
   if(!StructToCharArray(ExtRates[0], ExtCharArray))
     {
      Print("StructToCharArray() failed. Error code: "GetLastError());
      return;
     }
 
//--- cancella la struttura
   ZeroMemory(ExtRates[0]);
//--- stampa i dati nella struttura dopo la cancellazione
   Print("\nData in the structure after ZeroMemory():");
   MqlRatesPrint(ExtRates[0]);
 
//--- ora copia i dati dall'array uchar alla struttura MqlRates
   if(!CharArrayToStruct(ExtRates[0], ExtCharArray))
     {
      Print("CharArrayToStruct() failed. Error code: "GetLastError());
      return;
     }
//--- stampa i dati nella struttura MqlRates dopo averlo copiato dall'array uchar
   Print("\nData in the MqlRates structure after copying it from a uchar array:");
   MqlRatesPrint(ExtRates[0]);
 
   /*
  risultato:
 
   Data in the structure immediately after receiving it:
   MqlRates data:
    Time = 2024.02.21 07:00;
    Open = 1.08143;
    High = 1.08158;
    Low = 1.08122;
    Close = 1.08137;
    Tick volume = 1341;
    Spread = 4;
    Real volume = 0
 
   Data in the structure after ZeroMemory():
   MqlRates data:
    Time = 0;
    Open = 0.00000;
    High = 0.00000;
    Low = 0.00000;
    Close = 0.00000;
    Tick volume = 0;
    Spread = 0;
    Real volume = 0
 
   Data in the MqlRates structure after copying it from a uchar array:
   MqlRates data:
    Time = 2024.02.21 07:00;
    Open = 1.08143;
    High = 1.08158;
    Low = 1.08122;
    Close = 1.08137;
    Tick volume = 1341;
    Spread = 4;
    Real volume = 0
   */
  }
//+--------------------------------------------------------------------------------+
//| Stampa i campi della struttura MqlRates nel journal                            |
//+--------------------------------------------------------------------------------+
void MqlRatesPrint(MqlRates &rates)
  {
//--- crea la stringa di output
   string text=StringFormat(" Time = %s;\n"
                            " Open = %.*f;\n"
                            " High = %.*f;\n"
                            " Low = %.*f;\n"
                            " Close = %.*f;\n"
                            " Tick volume = %I64u;\n"
                            " Spread = %d;\n"
                            " Real volume = %I64u",
                            TimeToString(rates.time),
                            _Digitsrates.open,
                            _Digitsrates.high,
                            _Digitsrates.low,
                            _Digitsrates.close,
                            rates.tick_volume,
                            rates.spread,
                            rates.real_volume);
//-- stampa l'intestazione e la stringa di output nel journal
   Print("MqlRates data:\n"text);
  }

Vedi anche

StringToCharArray, ShortArrayToString,CharArrayToStruct, Uso di Codepage, FileWriteStruct, Unions (union), MathSwap