FileRead isn't working correctly - what can I do?

 

Hi

I want to read a newsFile, it exists and can be opened, but it is read only about 20%??

                // Open the file
                hdl = FileOpen(newsFile, FILE_BIN|FILE_READ);
                if (hdl < 0){
                        Print("Can\'t open xml file: ", newsFile, ".  The last error is ", GetLastError());
                        return(false);
                }
                if (DebugLevel > 0)
                        Print("XML file: ",FileSize(hdl)," open must be okay");
           szRead = FileSize(hdl);      // Read in the whole XML file
                sData = FileReadString(hdl, szRead);    
                if (DebugLevel > 0)
                Print("file: ",FileSize(hdl)," sData: ",StringLen(sData),"  compare: ",(StringLen(sData) < FileSize(hdl)),"  Err: ",GetLastError());
        
                if (hdl > 0)
                        FileClose(hdl);
                        
//and this produces:                    
2013.11.05 18:48:37     newsIndi EURUSD,M30: file: 26721 sData: 4095  compare: 1  Err: 0
2013.11.05 18:48:37     newsIndi EURUSD,M30: file: 26721 open must be okay

Why the hell does it read less than 20% of that file - information is missing.

BUT this happen only if I install that indi fresh on the chart, if I open the Edit the paramter window everything was perfect.

My special problem is if I call this indi via iCustom(..) I can't 'edit' its parameters..

What can I do?

Now I have inserted

if (StringLen(sData) < szRead) return(0);

and the indiread only 4k again and again..

Any hint?

Gooly

 
gooly:

Hi

I want to read a newsFile, it exists and can be opened, but it is read only about 20%??

Why the hell does it read less than 20% of that file - information is missing.

BUT this happen only if I install that indi fresh on the chart, if I open the Edit the paramter window everything was perfect.

My special problem is if I call this indi via iCustom(..) I can't 'edit' its parameters..

What can I do?

Now I have inserted

and the indiread only 4k again and again..

Any hint?

Gooly

It seems to me you are trying to read the entire file into a single string variable . . .

Functions working with strings try to increase internal buffer for string processing. Initial buffer size is 4K. Maximal buffer size depends on free memory.

from here: https://www.mql5.com/en/forum/46439
 

hmm, it could be that especially after it works perfectly after a re-start..

Thanks anyway!

Gooly

 

Well, the problem is pretty annoying, because at least something is read but of course in many cases (NOT every time!) something is wrong and there you start looking.

Especially after editing the parameters (that have nothing to to with the problem you see!) everything is fine - but an EA can't 'edit the parameters' and restart its EA!!

But here is my code that solves so far the problem:


           fHdl = FileOpen(sFileName, FILE_BIN|FILE_READ);
           szFile = FileSize(fHdl);
           GetLastError(); // elim prev Errors
           sData = ""; // re-ini this var.
           string strAdd=""; // 
           sData = FileReadString(fHdl,szFile); 
           Print("File ",szFile," opened, should be okay  sizeData: ",StringLen(sData));
           if ( StringLen(sData) < szFile ) {
                if (!FileSeek( fHdl, 0, SEEK_SET)) Print("FileSeekError: ",GetLastError()); 
                strAdd = sData;
                int m,mm = szFile/StringLen(sData) + 1;m=mm;
                while(m>0) { strAdd = strAdd + sData; m--; }
                strAdd = FileReadString(fHdl,szFile);
                sData = strAdd; 
                Print("File rep:",mm," szF:",szFile," szData:",StringLen(sData),
                      "  szCompare: ",(StringLen(sData) < szFile),"  ",GetLastError(),
                      " szAdd: ",StringLen(strAdd));
           }
           if (fHdl > 0) FileClose(fHdl);

this prints:

2013.11.06 14:27:08 FFCalv4 NZDUSD,M1: File 26770 opened, should be okay sizeData: 4095
2013.11.06 14:27:08 FFCalv4 NZDUSD,M1: File rep:7 szF:26770 szData:26770 szCompare: 0 0 szAdd: 26770

initial size (and read) 4095 so use the size increase loop.

But it is still very annoying,

Gooly

 
gooly:

Well, the problem is pretty annoying, because at least something is read but of course in many cases (NOT every time!) something is wrong and there you start looking.

Why do you think it makes sense to have a 4k long string ?
 

Don't ask me!

If you read my code you can see that after the first FileRead:

sData = FileReadString(fHdl,szFile);

only 4k were read while 26k should have been read: 2013.11.06 14:27:08 FFCalv4 NZDUSD,M1: File 26770 opened, should be okay sizeData: 4095

So I blow up the size of sData (with the help of strAdd) to more that the FileSize and now I can read the file totaly.

This way is NOT my choice!

Of course it can be done with less code, but I want to see the the various values.

Gooly

 
gooly:

Don't ask me!

I think you have misunderstood my question, probably my fault. What I meant was why are you trying to read all this data into one huge string ? wouldn't it make sense to use more smaller strings ?
 

1) This is the least time and code-line consuming way,
2) I did not expect to have such problems,
3) clear structure that separates parsing and file reading.

Gooly

PS: if you have nice code to read and parse I'll have a look at it.

 
gooly:

1) This is the least time and code-line consuming way,
2) I did not expect to have such problems,
3) clear structure that separates parsing and file reading.

Gooly

PS: if you have nice code to read and parse I'll have a look at it.

OK, fair enough . . .

Sorry, I have done some work with string parsing and manipulation but nothing along the lines of what you are looking at . . . don't you have a carriage return or linefeed you can use and have one string per line ?
 

Well,

if you use the mt4-file-finctions you ONLY can read x-bytes.

The carriage-return is just one char among the others - your editor simply does s.th different.

So if you read a bit of the file you don't know where is the and of you bit. May be it ends right in the middle of a token you are searching for.

To manage all this ... I think you can imagine..

Or you have to use the file-stuff of Windows as well a big portion of extra code.

 
gooly:

Well,

if you use the mt4-file-finctions you ONLY can read x-bytes.

The carriage-return is just one char among the others - your editor simply does s.th different.

So if you read a bit of the file you don't know where is the and of you bit. May be it ends right in the middle of a token you are searching for.

To manage all this ... I think you can imagine..

Or you have to use the file-stuff of Windows as well a big portion of extra code.

Did you look at existing code ? https://www.mql5.com/ru/code/10366