Working with files. - page 2

 
mql5:
Strings in MQL are unicode (2 bytes per character) and kernel32.dll functions are ansi (1 byte per character). Use a byte array and function StringToCharArray to call the ansish functions
How so?
As I recall, Windows is almost entirely Unicode, and has been for a long time...

Or is kernel32.dll an exception?

-----Added------

Looked at it, really ansi, strange...

 

A word of advice to an amateur. My knowledge of working with files is at the level of a Word user.

The Expert Advisor needs to overwrite one value of datetime type in the file, each time at the beginning of the file, during the whole time of its work. Read the last value written - only when restarting the Expert Advisor. I have made a simple construction, using .csv-file - everything seems to work. The following questions appeared:

1) What file type is better to use for saving datetime values in order to minimize time of writing procedure? As I understood, .csv-files work with strings, and where there are strings, there is additional time consumption for their processing.

2) How to correctly use FileClose(): should I close the file each time after writing a new value to it, or close it once, in OnDeinit() function? I would like to open the file once and then just write new values into it, without wasting time on opening-closing it multiple times. But is it safe to do so?

3) Do I correctly understand that if some value is written to the file, but the file is not closed, then in case of a sudden power outage this written value will not disappear, and when the program is loaded, it will be possible to read it later?

 

Yedelkin:

The following questions have come up:

1) What file type is better to use to save datetime values in order to minimize the write procedure time? As I understood, .csv-files work with strings, and where there are strings, there is additional time consumption for their processing.

2) How to correctly use FileClose(): should I close the file each time after writing a new value to it, or close it once, in OnDeinit() function? I would like to open the file once and then just write new values into it, without wasting time on opening-closing it multiple times. But is it safe to do so?

3) Do I correctly understand that if some value is written to the file, but the file is not closed, then in case of a sudden power outage this written value will not disappear, and when the program is loaded, it will be possible to read it later?

1. It depends on the format the file is saved in. You can save the date as a number, text or a datetime specialised type.

The second question would be this: Why do we save it to a file, who will view it and how?

Writing to TXT would be the easiest and most reliable option (you can read it from any program, or almost any program), CSV is a more advanced way to write to file. There are advantages, but there are also definite disadvantages.

2. I prefer to open once in OnInit or constructor of main class (depends on implementation) and close in OnDeinit or in destructor.

But if there is a need to reopen/reopen the file (there are a number of reasons for such actions), you can do it periodically (once every hour/day/week).

If the file is big or information in it will be difficult to restore, it is better to periodically overwrite it or create a new one.

3. if value was written but file is not closed correctly (sudden power cut or software hang-up) most likely data will be lost (partially or fully is a separate question).

I remember experimenting with writing to plain txt in a program written in Delphi. In the case of problems the last record was often beaten or missing.

 

A mql function returning the time of the last modification of the file would be very welcome.

datetime FileLastModificationTime(string FName);
 
MetaDriver:

A mql function returning the time of the last modification of the file would be very welcome.

datetime FileLastModificationTime(string FName); 
In general - a poet's dream!
 
Interesting:

1. the date can be saved as: number, text or special datetime type.

I couldn't find any function that saves date as datetime type. If only through arrays.

For some reason, it seems that it would be better to store values of datetime type in a binary file (the file itself is designed to be read only by the same Expert Advisor during reloading). I will try to experiment.

Interesting:

If value was written, but file was not closed correctly (sudden power cut or software hang-up) most likely the data will be lost (partially or completely is a separate question).

I remember experimenting with writing into plain txt in a program written in Delphi. There in case of problems the last record was often beaten or missing.

That's too bad. It turns out that if you want to guarantee saving of the last value you must use FileClose() function all the time :(

Документация по MQL5: Основы языка / Типы данных / Целые типы / Тип datetime
Документация по MQL5: Основы языка / Типы данных / Целые типы / Тип datetime
  • www.mql5.com
Основы языка / Типы данных / Целые типы / Тип datetime - Документация по MQL5
 
Yedelkin:

That's a pity. It turns out that if you want to ensure that the last written value is saved, you have to use FileClose() all the time :(

FileFlush() was invented for this purpose.
 

sergeev:

Yedelkin:

That's a pity. It turns out that if you want to guarantee saving the last value you wrote, you have to use FileClose() all the time :(

FileFlush() was invented for this purpose.

It may be. But it does not say anything how to use it (when to use it). It may be an easy question for a pro, but personally I did not see any special sense in FileFlush() after reading the documentation...

And the difference between FileClose() and FileFlush() is still unclear :/

FileFlush

Reset to disk all data remaining in the file I/O buffer.

...FileFlush() must be called between file read and file write operations.

So, there is no writing to file and data are already "flushed to disk" somewhere?

 
Yedelkin:

Possibly. But it doesn't say how to use it (when to use it). It may be an easy question for a pro, but personally, after reading the documentation I don't see any special sense in FileFlush()...

And the difference between FileClose() and FileFlush() is still unclear :/

So, there is no writing to the file, but the data are already "flushed" somewhere?

Here is a more detailed description and example from MQL4 Reference

void FileFlush( int handle)


Resets to disk all data remaining in the file I/O buffer.

Note: FileFlush() must be called between file read and file write operations.
When a file is closed, the data is automatically reset to disk, so there is no need to call FileFlush() before calling FileClose().
Parameters:
handle - File descriptor returned by FileOpen().

Example:

int bars_count=Bars;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
  
  if(handle>0)
    {
     FileWrite(handle, "#","OPEN","CLOSE","HIGH","LOW");
     for(int i=0;i<bars_count;i++)
       FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
     FileFlush(handle);
     ...
     for(int i=0;i<bars_count;i++)
       FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
     FileClose(handle);
    }

If I understood correctly, the FileFlush call, unlike FileClose, does not close the file, which allows to continue working with it. And in comparison with reopening, you should get a significant speed increase.

Although you need a more specific example of the task at hand.

FileFlush - Документация на MQL4
  • docs.mql4.com
FileFlush - Документация на MQL4
 
Interesting:

Here is a more detailed description with an example, from the MQL4 help

"When closing the file, the data is automatically reset to disk, so there is no need to call FileFlush() before calling FileClose()" - Yes, yes, I'm starting to see whatsergeev was talking about. So, it turns out that instead of FileClose() you can call FileFlush() to guarantee saving the last record to the file? And this would be a competent solution?
Reason: