There is a test script:
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| writeSpicifiedType.mq5 |
//| hoz |
//| |
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
#property copyright "hoz"
#property link ""
#property version "1.00"
/*
double high = 0;
double profit = 0;
double low = 0;
*/
double high = 1.3154;
double profit = 50;
double low = 1.3140;
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Script program start function |
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void OnStart() {
//---
Print("_high = ", DoubleToString(high));
Print("_profit = ", profit);
Print("_low = ", low);
loadSettings();
saveSettings();
Print("high = ", high);
Print("profit = ", profit);
Print("low = ", low);
}
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Expert Load setings function |
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void loadSettings(void) {
string fileName = _Symbol + ".dat";
int fileHandle;
//---
if (FileIsExist(fileName, 0)) {
fileHandle = FileOpen(fileName, FILE_READ|FILE_BIN);
if (fileHandle != INVALID_HANDLE) {
high = FileReadDouble(fileHandle);
Print(__FUNCTION__, " high = ", high);
profit = FileReadDouble(fileHandle);
Print(__FUNCTION__, " profit = ", profit);
low = FileReadDouble(fileHandle);
Print(__FUNCTION__, " low = ", low);
FileClose(fileHandle);
}
}
}
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Expert Save settings function |
//+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void saveSettings(void) {
string fileName = _Symbol + ".dat";
int fileHandle;
bool fileFound = true;
//---
if (FileIsExist(fileName, 0)) {
if (FileDelete(fileName, 0))
fileFound = false;
} else {
fileFound = false;
}
//---
if (!fileFound) {
fileHandle = FileOpen(fileName, FILE_WRITE|FILE_BIN);
if (fileHandle != INVALID_HANDLE) {
FileWriteDouble(fileHandle, high);
FileWriteDouble(fileHandle, profit);
FileWriteDouble(fileHandle, low);
FileClose(fileHandle);
}
}
}
In OnStart (), I call loadSettings() and saveSettings () in turn, respectively, to read data and write data. Everything is elementary and simple. But for some reason, when reading, the data is not read. What is this supposed to mean?
hoz
XS. Overloaded the terminal and everything now works. Glyukanul apparently accidentally. The question is removed.
barabashkakvn
Like I wrote the most detailed example:
//+------------------------------------------------------------------+
//| writeSpicifiedType.mq5 |
//| hoz |
//| |
//+------------------------------------------------------------------+
#property copyright "hoz"
#property link ""
#property version "1.00"
//---
double high=-1.0;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
Print("");
ReadSettings();
Print("read high = ",DoubleToString(high,5));
high=rand()/32767.0+1.0;
Print("rand high = ",DoubleToString(high,5));
WriteSettings();
}
//+------------------------------------------------------------------+
//| Read setings function |
//+------------------------------------------------------------------+
void ReadSettings(void)
{
Print(__FUNCTION__);
string fileName=_Symbol+".dat";
int fileHandle;
//---
if(FileIsExist(fileName,0))
{
Print("FileIsExist=true");
fileHandle=FileOpen(fileName,FILE_READ|FILE_BIN);
if(fileHandle!=INVALID_HANDLE)
{
Print("FileOpen=true");
high=FileReadDouble(fileHandle);
FileClose(fileHandle);
}
else
Print("FileOpen=false");
}
else
Print("FileIsExist=false");
}
//+------------------------------------------------------------------+
//| Write settings function |
//+------------------------------------------------------------------+
void WriteSettings(void)
{
Print(__FUNCTION__);
string fileName=_Symbol+".dat";
int fileHandle;
bool fileFound=true;
//---
if(FileIsExist(fileName,0))
{
Print("FileIsExist=true");
if(FileDelete(fileName,0))
{
Print("FileDelete=true");
fileFound=false;
}
else
Print("FileDelete=false");
}
else
{
Print("FileIsExist=false");
fileFound=false;
}
//---
if(!fileFound)
{
Print("fileFound=false");
fileHandle=FileOpen(fileName,FILE_WRITE|FILE_BIN);
if(fileHandle!=INVALID_HANDLE)
{
Print("FileOpen=true");
FileWriteDouble(fileHandle,high);
FileClose(fileHandle);
}
else
Print("FileOpen=false");
}
else
Print("fileFound=true");
}
//+------------------------------------------------------------------+
And the result (three runs, before the first run of the file is not yet)
ReadSettings
FileIsExist=false
read high = -1.00000
rand high = 1.92523
WriteSettings
FileIsExist=false
fileFound=false
FileOpen=true
ReadSettings
FileIsExist=true
FileOpen=true
read high = 1.92523
rand high = 1.09458
WriteSettings
FileIsExist=true
FileDelete=true
fileFound=false
FileOpen=true
ReadSettings
FileIsExist=true
FileOpen=true
read high = 1.09458
rand high = 1.61129
WriteSettings
FileIsExist=true
FileDelete=true
fileFound=false
FileOpen=true