Is it correct?
Please read documentation on FileOpen() function, especially example code.
There are examples of "incorrect" and "correct" file opening code.
You use "incorrect" one and besides that looks like you have a bug.
When fails, FileOpen() returns INVALID_HANDLE, which is -1, so your code probably correctly opens file (since you can see it in the folder), but you incorrectly conclude that FileOpen() failed.
//+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- incorrect file opening method string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH); string filename=terminal_data_path+"\\MQL4\\Files\\"+"fractals.csv"; int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV); if(filehandle<0) { Print("Failed to open the file by the absolute path "); Print("Error code ",GetLastError()); } //--- correct way of working in the "file sandbox" ResetLastError(); filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV); if(filehandle!=INVALID_HANDLE) { FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period))); FileClose(filehandle); Print("FileOpen OK"); } else Print("Operation FileOpen failed, error ",GetLastError()); //--- another example with the creation of an enclosed directory in MQL4\Files\ string subfolder="Research"; filehandle=FileOpen(subfolder+"\\fractals.txt",FILE_WRITE|FILE_CSV); if(filehandle!=INVALID_HANDLE) { FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period))); FileClose(filehandle); Print("The file most be created in the folder "+terminal_data_path+"\\"+subfolder); } else Print("File open failed, error ",GetLastError()); }
Hi,
I've changed my code:
... fichero=FileOpen(str,FILE_TXT|FILE_WRITE); ... if (fichero==INVALID_HANDLE) ...
and now is working properly.
Thank you very much both for your help.
Best Regards,
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
Dear all,
I created my first code for creating and writinf a file using mql4. I want to be sure it works properly so I decided to write some welcome words every time I execute it. Sadly, the file is correctly created but nothing is written in it. I attach my code for the community to take a humble look. I guess it must be a dumb error, but I simply can't see it. Here it is (it's in Spanish).
Thank you very much in advance.