Cannot read from or write to file in MT5

 

I just recently switched to MT5. I did not have any issue issue reading or writing to files in MT4. I put the file in MetaQuotes\Terminal\<terminal id>\tester\files and it would work no problem.


For MT5, I tried to put my file in C:\Users\user\AppData\Roaming\MetaQuotes\Terminal\Common\Files and also in C:\Users\user\AppData\Roaming\MetaQuotes\Terminal\<terminal id>\MQL5\Files\ and still nothing seems to work. I need some help. My EA needs a helper file to work properly.

Documentation on MQL5: File Functions / FileWrite
Documentation on MQL5: File Functions / FileWrite
  • www.mql5.com
The function is intended for writing of data into a CSV file, delimiter being inserted automatically unless it is equal to 0. After writing into...
 
Nick McKenzie:

I just recently switched to MT5. I did not have any issue issue reading or writing to files in MT4. I put the file in MetaQuotes\Terminal\<terminal id>\tester\files and it would work no problem.


For MT5, I tried to put my file in C:\Users\user\AppData\Roaming\MetaQuotes\Terminal\Common\Files and also in C:\Users\user\AppData\Roaming\MetaQuotes\Terminal\<terminal id>\MQL5\Files\ and still nothing seems to work. I need some help. My EA needs a helper file to work properly.

Please share a code snippet where you read/write the file.

 
Nick McKenzie:

I just recently switched to MT5. I did not have any issue issue reading or writing to files in MT4. I put the file in MetaQuotes\Terminal\<terminal id>\tester\files and it would work no problem.


For MT5, I tried to put my file in C:\Users\user\AppData\Roaming\MetaQuotes\Terminal\Common\Files and also in C:\Users\user\AppData\Roaming\MetaQuotes\Terminal\<terminal id>\MQL5\Files\ and still nothing seems to work. I need some help. My EA needs a helper file to work properly.

does not work is meaningless,  what is the problem? what error code/message do you get?  show your source code

 
Paul Anscombe #:

does not work is meaningless,  what is the problem? what error code/message do you get?  show your source code

MT5 Test-Agents (Testers) have their own folder structure.

You might want to read about " tester_file" here:

https://www.mql5.com/en/docs/basis/preprosessor/compilation

Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
  • www.mql5.com
Every mql5-program allows to specify additional specific parameters named #property that help client terminal in proper servicing for programs...
 

There is a debug print statement that's always get printed if the file cannot be be opened. The error message shows up every time I run the EA and the contents of the file are not there. The exact same code worked in MT4. I also tried to debug myabe I put the file in a wrong directory by writing to a debug.txt and search for it and I couldn't find it anywhere.

string str;
string write_file_name="debug.txt";
string read_file_name;
string path = "C:\\Users\\<user>\\AppData\\Roaming\\MetaQuotes\\Terminal\\<terminal id>\\MQL5\\Files\\";

int read_file_handle;
int write_file_handle;
int string_length;

read_file_name = path + FILE_NAME + ".txt";

if(c::test_write_file) {
        write_file_name = path + write_file_name;
        write_file_handle=FileOpen(write_file_name, FILE_WRITE);
        FileWriteString(write_file_handle, "testing");
        FileClose(write_file_handle);
        Print("test write file to " + write_file_name);
}

current_time = TimeCurrent();
current_time = current_time + (60 * 60 * c::TIME_OFFSET);
day_of_week = TimeDayOfWeek(current_time);

if((current_time > last_file_read_time && Minute() > 31) || last_file_read_time == 0 || disable_reread_file) {

        read_file_handle=FileOpen(read_file_name, FILE_READ);

        if(read_file_handle!=INVALID_HANDLE) {
                current_header=FileReadString(read_file_handle);
                Print("reading from " + read_file_name + " with header " + current_header);
                while(!FileIsEnding(read_file_handle)) {
                        str=FileReadString(read_file_handle);
                }
        } else {
                Print(read_file_name + " not found");
                file_read_error = true;
        }
        
        FileClose(read_file_handle);
}

return;
 
string path = "C:\\Users\\<user>\\AppData\\Roaming\\MetaQuotes\\Terminal\\<terminal id>\\MQL5\\Files\\";

read_file_name = path + FILE_NAME + ".txt";

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)