FileReadArray and FileWriteArray

To add comments, please log in or register
zhanglini
26
zhanglini  

Hey

I am new to MQL.  I have the hardest time trying to get FileReadArray and FileWriteArray to work, nothing is written or read.  My goal is to read/write an array ("NextRunTime",datatype = datetime) from/to a file.  Below is a portion of my code, the the "NRT.log" exists, and I don't have problem read/write to other files in the folder (none array types):  Thanks in advance!

=================Write=========================

int filehandle2=FileOpen("NRT.log",FILE_READ|FILE_WRITE|FILE_COMMON|FILE_TXT,';');

if(filehandle2  !=INVALID_HANDLE) 
     { 
FileWriteArray(filehandle2, NextRunTime,0,WHOLE_ARRAY ); 
FileClose(filehandle2);
SendMail("Array Written", " ");}//NRT.log ends
else{SendMail("File open error ", " ");}

--only when I set FILE_BIN, I can see some junk in the file, when I use TXT or CSV I see nothing.

   ================= Read========================
   int filehandleArray = FileOpen("NRT.log",FILE_READ|FILE_COMMON|FILE_TXT);
   if(filehandleArray  !=INVALID_HANDLE) 
     { 
   FileReadArray(filehandleArray,NextRunTime,0,WHOLE_ARRAY );

FileClose(filehandleArray);} else {SendMail("Can't OPen file","");}

------ when I SendMail(NextRunTime[1],[2] [3]  etc), I can see the initial data, not the data in the file.  I also don't know what the array file should look like, everything in one line with delimiters, or one entry per line?


By the way, I use this script to loop through multiple timeframe and multiple symbols, when a certain timeframe has signals, it sends out an email to me, and then won't run for the timeframe until the "NextRunTime", which is Current plus the length of the TimeFrame.  Right now it is running fine without the file, but I want the file, so the program can take breaks during weekends, nights etc, and inherit the times using the file when I restart it.  I hope you understand what I am doing here.  


Thank you!!!


---PS: I was able to write to the file using a loop; if only I can write in the format ReadArray likes and can read.  So if I can't fix the WriteArray problem, as long as I know the format the file should have, and fix the ReadArray problem, I shall be fine.  Thanks

zhanglini
26
zhanglini  

I guess asking questions over the weekend is not a good thing, lol.  Anyone can tell me what I did wrong with Read/WriteArray?

I am still curious if anyone has the answer.  But I have solved the problem by writing into the file a string, then reading the string from the file (when I restart the script), and then substr into the array.  It works fine now.  There are always different ways to do things.

Thanks

Petr Nosek
1526
Petr Nosek  
zhanglini:

I guess asking questions over the weekend is not a good thing, lol.  Anyone can tell me what I did wrong with Read/WriteArray?

I am still curious if anyone has the answer.  But I have solved the problem by writing into the file a string, then reading the string from the file (when I restart the script), and then substr into the array.  It works fine now.  There are always different ways to do things.

Thanks

You should read documentation:

https://www.mql5.com/en/docs/files/filewritearray

https://www.mql5.com/en/docs/files/filereadarray

Both functions work only with BIN files (not TXT)

nicholi shen
2159
nicholi shen  
zhanglini:

I guess asking questions over the weekend is not a good thing, lol.  Anyone can tell me what I did wrong with Read/WriteArray?

I am still curious if anyone has the answer.  But I have solved the problem by writing into the file a string, then reading the string from the file (when I restart the script), and then substr into the array.  It works fine now.  There are always different ways to do things.

Thanks

I skipped over your post because you didn't format your code, and I'm sure many others did as well. Your issue is that you are trying to use functions intended for R/W binary files -- on text files. 

To add comments, please log in or register