5008 wrong file handle (handle index is out of handle table)

 

The code appears to executes OK ( i.e. reads details from file and My_Handle_line seems OK - 2) -but still generates the ERROR code 5008

My_Handle_Line  = FileOpen("Dashboard\\"+fname, FILE_CSV|FILE_READ, "|");          
                        Write_my_Error("80010A My_Handle_Line = "+DoubleToString(My_Handle_Line,0));  FileFlush(My_Handle);
//..........................................................................................................................
   if( My_Handle_Line > -1 ) 
   {  str1 = FileReadString(My_Handle_Line);          Write_my_Error("80010D Test");  FileFlush(My_Handle);

The following is from my own log file ~ but don't know how to resolve

>>>>>>>>>>>.... 80010A My_Handle_Line = 2 My_error: 5008 wrong file handle (handle index is out of handle table)

I don't get an error resulting from code below and str1 is read correctly

FileReadString(My_Handle_Line)

 
You open a file. Do you ever close the handle?
 
WHRoeder:
You open a file. Do you ever close the handle?

Yes I close files etc ...... found the problem ..... I used FILEFLUSH 0n the read file.


Didn't find the actual error message very helpful - perhaps just a pointer in the right direction


Thanks again - as always - for your speedy response

 
Demos:

hello there :)

I would like to ask about that line, if you do not mind. I can see that you are testing for the File Handle to be 0 or more; and here is my question: a file handle in MQL is a integer, as we all know. That integer presummably stores the address of the location of the file buffer (a pointer in C) - it may not be a label or something. So two things

a) Let us assume that this number  is the address in the hdd/ssd. Then i doubt it can be 0, as there is the OS anyway

b) If it is a "pointer" in a memory place, that serves as the start of the file buffer, then this place can not be 0: Windows will not let you write in the physical address 0 (NULL pointer is not accessible in Windows - see https://en.wikipedia.org/wiki/Zero_page )

To conclude, i believe you can write if( My_Handle_Line > 0 ) 

best regards 

The MQL file handle is valid only for the current script and handles are numbered 1, 2, 3 etc., not related to anything you assume.
 
Ovo:
The MQL file handle is valid only for the current script and handles are numbered 1, 2, 3 etc., not related to anything you assume.

ok, after looking at the manual i realised that MQL likely indexes the handles that obtains (from Windows) as file handles and offers those indices as handles; or something in that line hehe -  ihave to have a look at Win32 API

A final remark, if numbering starts from 1 and not 0 then it should be My_Handle_Line > 0 and not -1 right ?

best regards 

 
Demos: if numbering starts from 1 and not 0 then it should be My_Handle_Line > 0 and not -1 right ?
Wrong. RTFM
  1. FileOpen - File Functions - MQL4 Reference states that on a error it returns INVALID_HANDLE (-1)
  2. No where does it states that valid handles starts at 1. (Same as ticket numbers)
 
WHRoeder:
Wrong. RTFM
  1. FileOpen - File Functions - MQL4 Reference states that on a error it returns INVALID_HANDLE (-1)
  2. No where does it states that valid handles starts at 1. (Same as ticket numbers)

Allright, i was looking somewhere else in the reference and did not see that one. Meanwhile, i was looking around the internet and found that one 

https://blogs.msdn.microsoft.com/oldnewthing/20040302-00/?p=40443 

it says a thing or two about why CreateFile returns -1 in Windows API and not 0. Back OT, i printed the handle of the first file opened in a EA and guess what, it was number 1 :) So maybe fail code can be -1 or 0, depending on the function but handles likely start at 1 in MQL

 
Demos:

Allright, i was looking somewhere else in the reference and did not see that one. Meanwhile, i was looking around the internet and found that one 

https://blogs.msdn.microsoft.com/oldnewthing/20040302-00/?p=40443 

it says a thing or two about why CreateFile returns -1 in Windows API and not 0. Back OT, i printed the handle of the first file opened in a EA and guess what, it was number 1 :) So maybe fail code can be -1 or 0, depending on the function but handles likely start at 1 in MQL 

Give up such effort, MQL has a constant INVALID_HANDLE, while windows have INVALID_HANDLE_VALUE, both substitute for -1.

Why would any coder look for another undocumented value like you? 
 
Ovo:

Give up such effort, MQL has a constant INVALID_HANDLE, while windows have INVALID_HANDLE_VALUE, both substitute for -1.

Why would any coder look for another undocumented value like you? 

Hi Guys


Didn't intend to start an argument but thanks both your comments

WHRoeder - thanks again