
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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
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.
A mql function returning the time of the last modification of the file would be very welcome.
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.
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 :(
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 :(
sergeev:
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 :/
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?
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
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().
Example:
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.
Here is a more detailed description with an example, from the MQL4 help