Reading from a file during testing
Solution: You need to use Kernel32, I will include it here (Working with Metatrader 5 64bit)
#import "kernel32.dll" long CreateFileW( string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, uint hTemplateFile ); bool WriteFile( long hFile, uchar& lpBuffer[], uint nNumberOfBytesToWrite, uint& lpNumberOfBytesWritten, long lpOverlapped ); bool ReadFile( long hFile, uchar& lpBuffer[], uint nNumberOfBytesToRead, uint& lpNumberOfBytesRead, long lpOverlapped ); bool CloseHandle(long hObject); long GetFileSize(int hFile, long lpFileSizeHigh); void CopyMemory(string &dest, uchar &src[], int length); #import string ReadFromFile(string fileName) { // Open the file with read permissions long hFile = CreateFileW(fileName, GENERIC_READ, _FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if(hFile == -1) { // Print("Failed to open the file."); return(""); } // Define a buffer to read the file contents uchar wideBuffer[]; ArrayResize(wideBuffer,(int)GetFileSize((int)hFile,0)); uint bytesRead; if(!ReadFile(hFile, wideBuffer, ArraySize(wideBuffer), bytesRead, 0)) { // Print("Failed to read from the file."); CloseHandle(hFile); return(""); } CloseHandle(hFile); return CharArrayToString(wideBuffer); } void WriteData(char &data[],string path){ long hFile = CreateFileW(path, GENERIC_WRITE, _FILE_SHARE_READ | _FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if(hFile != -1) { if(WriteFile(hFile, data, ArraySize(data), bytesWritten, 0)) { //Do something when write file successfully } CloseHandle(hFile); } ZeroMemory(data); ArrayFree(data); }Hope you can find it useful.
Reading from a file during testing is not easy to solve.
I am getting error:
Here is my code:
Any idea how to solve it?
Why did you comment out the property tester_file? This would be the easiest way to access the file by short name just in the agent directory, because such files are transferred to agents automatically.
Also you can open local files in the common MQL folder.
string source_file = TerminalInfoString(TERMINAL_PATH) + "\\MQL5\\Files\\trading_signals.csv"; string destination_file = current_directory + "\\MQL5\\Files\\trading_signals.csv";Stop specifying a path. File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)
and FolderDelete using TERMINAL_DATA_PATH - General - MQL5 programming forum (2017)
Finally, this was achieved by placing the csv file in a common path and setting a flag FILE_COMMON to open the file.
the property tester directive works but you have to compile then restart mt5 first.
Reading from a file during testing is not easy to solve.
I am getting error:
Here is my code:
Any idea how to solve it?