Creating an external file generates characters I don't understand.(UNICODE, ANSI).

 

Goodnight, I'm creating a bot and I need to extract some information for analysis, so I'm trying to create a txt, or csv, or even bin file, whatever I can make it work to receive this information; everything seems to work fine, as stated in the documentation, however the generated file has characters that I don't understand. I believe the error is in the FileOpen() command flags, the generated file seems to be in unicode, however, even specifying the FILE_ANSI flags the problem persists. I then used an example code from the documentation to test the situation, and in the same way the result was an unreadable file. in the "file open flags" tab of the documentation, there is a caveat, "If a file is opened for reading as a text file (FILE_TXT or FILE_CSV), and at the beginning of the file a two-byte indication 0xff,0xfe is encountered, the encoding flag will be FILE_UNICODE, even if FILE_ANSI is specified." https://www.mql5.com/en/docs/constants/io_constants/fileflags I believe this is the problem this "double byte indication" prevents me from encoding to ANSI, however I don't know how to react to this. here below I leave the example that I adapted from the documentation that even specifying FILE_ANSI, I believe it still returns me a writing in UNICODE. Reference of the example in the documentation: https://www.mql5.com/en/docs/files/filewritestruct Thank you all for the attention,


//+------------------------------------------------------------------+
//|                                          Demo_FileWiteStruct.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- mostrar a janela de parâmetros de entrada quando do lançamento do script
#property script_show_inputs
//--- parâmetros para a recepção de dados a partir do terminal
input string          InpSymbolName="EURUSD";           // par de moedas
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_H1;        // time frame
input datetime        InpDateStart=D'2013.01.01 00:00'; // data de início da cópia dos dados
//--- parâmetros para escrever dados no arquivo
input string          InpFileName="EURUSD.txt";         // nome do arquivo
input string          InpDirectoryName="Data";          // nome do diretório
//+------------------------------------------------------------------+
//| Estrutura para armazenar dados candlestick                       |
//+------------------------------------------------------------------+
struct candlesticks
  {
   double            open;  // preço de abertura
   double            close; // preço de fechamento
   double            high;  // preço de máximo
   double            low;   // preço de mínimo
   datetime          date;  // data
  };
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  {
   datetime     date_finish=TimeCurrent();
   int          size;
   datetime     time_buff[];
   double       open_buff[];
   double       close_buff[];
   double       high_buff[];
   double       low_buff[];
   candlesticks cand_buff[];
//--- redefine o valor de erro
   ResetLastError();
//--- receber o tempo da chegada das barras a partir do intervalo
   if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
     {
      PrintFormat("Falha para copiar valores de tempo. Código de erro = %d",GetLastError());
      return;
     }
//--- receber os preços de máximo das barras a partir do intervalo
   if(CopyHigh(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,high_buff)==-1)
     {
      PrintFormat("Falha ao copiar os valores dos preços de máximo. Código de erro = %d",GetLastError());
      return;
     }
//--- receber os preços de mínimo das barras a partir do intervalo
   if(CopyLow(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,low_buff)==-1)
     {
      PrintFormat("Falha ao copiar os valores dos preços de mínimo. Código de erro = %d",GetLastError());
      return;
     }
//--- receber os preços de abertura das barras a partir do intervalo
   if(CopyOpen(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,open_buff)==-1)
     {
      PrintFormat("Falha ao copiar os valores dos preços de abertura. Código de erro = %d",GetLastError());
      return;
     }
//--- receber os preços de fechamento das barras a partir do intervalo
   if(CopyClose(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,close_buff)==-1)
     {
      PrintFormat("Falha ao copiar os valores dos preços de fechamento. Código de erro = %d",GetLastError());
      return;
     }
//--- definir o tamanho de arrays
   size=ArraySize(time_buff);
//--- salvar todos os dados na estrutura array
   ArrayResize(cand_buff,size);
   for(int i=0;i<size;i++)
     {
      cand_buff[i].open=open_buff[i];
      cand_buff[i].close=close_buff[i];
      cand_buff[i].high=high_buff[i];
      cand_buff[i].low=low_buff[i];
      cand_buff[i].date=time_buff[i];
     }
 
//--- abrir o arquivo para escrever a estrutura array para o arquivo (se o arquivo estiver ausente, ele será criado automaticamente)
   ResetLastError();
   

int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON|FILE_ANSI);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s arquivo está aberto para escrita",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- preparar o contador do número de bytes
      uint counter=0;
      //--- escrever valores array no loop
      for(int i=0;i<size;i++)
         counter+=FileWriteStruct(file_handle,cand_buff[i]);
      PrintFormat("%d bytes de informação está escrito para %s arquivo",InpFileName,counter);
      PrintFormat("Número total de bytes: %d * %d * %d = %d, %s",size,5,8,size*5*8,size*5*8==counter ? "Correto" : "Erro");
      //--- fechar o arquivo
      FileClose(file_handle);
      PrintFormat("Os dados são escritos, %s arquivo esta fechado",InpFileName);
     }
   else
      PrintFormat("Falha para abrir %s arquivo, Código de erro = %d",InpFileName,GetLastError());
  }
   
  }
//+------------------------------------------------------------------+
 
cassiosantos: Reference of the example in the documentation: https://www.mql5.com/en/docs/files/filewritestruct T

Perhaps you should read the manual. FileWriteStruct is only for binary files.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

Reason: