Libraries: Library of functions for working with INI-files.

 

Library of functions for working with INI-files.:

The library contains five functions for reading the values from an INI-file and five functions for writing the values to an INI-file. Thus, there are five pairs of functions to read/write the values of 'integer array', 'boolean', 'double', 'integer' and 'string' types.

Author: Igor Kim

 

Hi Igor, This does not work. I use Win7 Professional (64 bit). There is message "uninit reason 0" but no file recorded.

Here is sample script that I run, but no luck to generate "MyFirst.ini" file.

//+------------------------------------------------------------------+
//|                                          ReadwriteIni_sample.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <IniFiles.mqh>

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

    string theFile = "C:\\MetaTrader4\\_temp_02\\tester\\MyFirst.ini";
    WriteIniString( theFile , "FirstSection" , "VarValue" , "HelloWorld" );
   
    Print("*** From the INI attempt ***");
   
  }
//+------------------------------------------------------------------+
 

https://docs.mql4.com/files/fileopen:

"For security reasons, work with files is strictly controlled in the MQL4 language. Files with which file operations are conducted using MQL4 means, cannot be outside the file sandbox."

Try it with a local filename (theFile = "MyFirst.ini") and check in the Files folder if it appears there. (<MT4 installation folder>\MQL4\Files)

FileOpen - File Functions - MQL4 Reference
FileOpen - File Functions - MQL4 Reference
  • docs.mql4.com
[in]  The name of the file can contain subfolders. If the file is opened for writing, these subfolders will be created if there are no such ones. [in]  value to be used as a separator in txt or csv-file. If the csv-file delimiter is not specified, the default delimiter is ";". If the txt-file delimiter is not specified, then no separator is...
 
lippmaje:

https://docs.mql4.com/files/fileopen:

"For security reasons, work with files is strictly controlled in the MQL4 language. Files with which file operations are conducted using MQL4 means, cannot be outside the file sandbox."

Try it with a local filename (theFile = "MyFirst.ini") and check in the Files folder if it appears there. (<MT4 installation folder>\MQL4\Files)

I can confirm that this is not working on my instance too,

Windows 10 x64,

WriteIniBool("MyticalBot.ini", "BOT", "OnOFF", botIsOn);
WriteIniDouble("MyticalBot.ini", "BOT", "Lots", lotSize);

As you can see, i'm trying to write local, but no file shows up at all.

 

The method calling windows api modified.

#property library

#import "kernel32.dll"
  int GetPrivateProfileStringA
      ( char& SectionName[],    
        char& KeyName[],        
        char& Default[],        
        char& ReturnedString[], 
        int    nSize,          
        char& FileName[]);      

#import


These codes can work normally:

string ReadIniString(string FileName, string SectionName, string KeyName,

                     string Default = "")

  {

   string ReturnedString;

   StringInit(ReturnedString,1024, 0); 

 

 char sname[1024], kname[1024], defchar[1024], retchar[1024], fname[1024];

  StringToCharArray( FileName, fname);

    StringToCharArray( SectionName, sname);

    StringToCharArray( KeyName, kname);

    StringToCharArray( Default, defchar);

    StringToCharArray( ReturnedString, retchar);


   int nValue = GetPrivateProfileStringA(sname, kname, defchar,

                                          retchar, 1024, fname);

   if(nValue > 0)

   {

       ReturnedString=CharArrayToString(retchar);

       return(ReturnedString);

   }

   else

       return(Default);

  }

 
Cheng Li:

The method calling windows api modified.

#property library

#import "kernel32.dll"
  int GetPrivateProfileStringA
      ( char& SectionName[],    
        char& KeyName[],        
        char& Default[],        
        char& ReturnedString[], 
        int    nSize,          
        char& FileName[]);      

#import


These codes can work normally:

string ReadIniString(string FileName, string SectionName, string KeyName,

                     string Default = "")

  {

   string ReturnedString;

   StringInit(ReturnedString,1024, 0); 

 

 char sname[1024], kname[1024], defchar[1024], retchar[1024], fname[1024];

  StringToCharArray( FileName, fname);

    StringToCharArray( SectionName, sname);

    StringToCharArray( KeyName, kname);

    StringToCharArray( Default, defchar);

    StringToCharArray( ReturnedString, retchar);


   int nValue = GetPrivateProfileStringA(sname, kname, defchar,

                                          retchar, 1024, fname);

   if(nValue > 0)

   {

       ReturnedString=CharArrayToString(retchar);

       return(ReturnedString);

   }

   else

       return(Default);

  }

This is still not compiling in metatrader 5, can you provide the full code that would compile in mt5 please? thanks

 

It doesn't work i put this ini file in MQL4/Files folder as you can see in this image:

and write this code in a script:

#include <IniFiles.mqh>

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   double test;
   test=ReadIniDouble("test.ini","Section","test");

   Comment(test);
  }

and compile it without any error but output is: 0.0