Download MetaTrader 5

FileOpen returns -1

To add comments, please log in or register
WOODY
8
WOODY  

Hello,

I am trying to retrieve a simple double value from a "data.txt" file in the following function.
However the FileOpen() function always returns -1 (INVALID_HANDLE).

The "data.txt" only contains 1 value (i.e. 89.55) and is located at MQL4/Files.

double ReadNumberFromFile()
{
   int file_handle;   
   double val;
   file_handle = FileOpen("data.txt",FILE_READ|FILE_TXT);
   Print(file_handle);
   val = FileReadDouble(file_handle);
   FileClose(file_handle);
      
   return val;
}

  

So is the file in a wrong directory or something?
Does anyone have an idea of whats wrong?

Thanks in advance

 

Woody 

Keith Watford
Moderator
9996
Keith Watford  

Make sure that you do not have the file open when running the code.

Otherwise you will need to use at least one of the SHARE flags

whroeder1
15023
whroeder1  
Woody: So is the file in a wrong directory or something?
  1. Verify it's in the <DataDirectory>MQL4\files or <DataDirectory>\tester\files depending on live vs. tester.
    Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014
  2. Don't just check the return code capture the last error also.
    What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4100

    ERR_SOME_FILE_ERROR

    Some file error

    4101

    ERR_WRONG_FILE_NAME

    Wrong file name

    4102

    ERR_TOO_MANY_OPENED_FILES

    Too many opened files

    4103

    ERR_CANNOT_OPEN_FILE

    Cannot open file

    4104

    ERR_INCOMPATIBLE_FILEACCESS

    Incompatible access to a fil

To add comments, please log in or register