my questions :
why size is 7 ?
why cnt is 6?
anyone please help to give some idea ?
anyone please help to give some idea ?
This is the way the variables change, sequence is from top to bottom . . .
2012.10.15 08:36:15 sergery USDCHF,H1: loaded successfully
2012.10.15 08:36:17 sergery USDCHF,H1: initialized
2012.10.15 08:36:17 sergery USDCHF,H1: FileSize(handle) = 20
2012.10.15 08:36:17 sergery USDCHF,H1: size = ArraySize(data) - size = 1
2012.10.15 08:36:17 sergery USDCHF,H1: ArrayResize(data,size+1) - size + 1 = 2
2012.10.15 08:36:17 sergery USDCHF,H1: cnt++ - cnt = 1
2012.10.15 08:36:17 sergery USDCHF,H1: size = ArraySize(data) - size = 2
2012.10.15 08:36:17 sergery USDCHF,H1: ArrayResize(data,size+1) - size + 1 = 3
2012.10.15 08:36:17 sergery USDCHF,H1: cnt++ - cnt = 2
2012.10.15 08:36:17 sergery USDCHF,H1: size = ArraySize(data) - size = 3
2012.10.15 08:36:17 sergery USDCHF,H1: ArrayResize(data,size+1) - size + 1 = 4
2012.10.15 08:36:17 sergery USDCHF,H1: cnt++ - cnt = 3
2012.10.15 08:36:17 sergery USDCHF,H1: size = ArraySize(data) - size = 4
2012.10.15 08:36:17 sergery USDCHF,H1: ArrayResize(data,size+1) - size + 1 = 5
2012.10.15 08:36:17 sergery USDCHF,H1: cnt++ - cnt = 4
2012.10.15 08:36:17 sergery USDCHF,H1: size = ArraySize(data) - size = 5
2012.10.15 08:36:17 sergery USDCHF,H1: ArrayResize(data,size+1) - size + 1 = 6
2012.10.15 08:36:17 sergery USDCHF,H1: cnt++ - cnt = 5
2012.10.15 08:36:17 sergery USDCHF,H1: size = ArraySize(data) - size = 6
2012.10.15 08:36:17 sergery USDCHF,H1: ArrayResize(data,size+1) - size + 1 = 7
2012.10.15 08:36:17 sergery USDCHF,H1: cnt++ - cnt = 6
2012.10.15 08:36:17 sergery USDCHF,H1: size = 7 cnt = 6
2012.10.15 08:36:17 sergery USDCHF,H1: 1
2012.10.15 08:36:17 sergery USDCHF,H1: 2
2012.10.15 08:36:17 sergery USDCHF,H1: 3
2012.10.15 08:36:17 sergery USDCHF,H1: 4
2012.10.15 08:36:17 sergery USDCHF,H1: 5
2012.10.15 08:36:17 sergery USDCHF,H1: 0
2012.10.15 08:36:17 sergery USDCHF,H1: 0
cnt is 6 and not 5 because you increment it during the final run of the loop, but anyway . . . cnt should be 4 not 5 as it starts at 0.
To see the EOF marker you have to read the next element . . then you see you are at the EOF. When you do this it is already too late and your loop has run 2 more times than it needed . . .
It's a little hard for me to explain . . . take a look at this modification to your code . . .
Print("FileSize(handle) = ",FileSize(handle)); dat = FileReadInteger(handle,LONG_VALUE); // read the first entry in the file while(FileIsEnding(handle)==false) { size = ArraySize(data); Print("size = ArraySize(data) - size = ", size); if(size>0) { data[size-1]=dat; // write the entry from the file into the array dat = FileReadInteger(handle,LONG_VALUE); // read the next entry in the file if(FileIsEnding(handle)) break; // if we have just read past the last element to the EOF quit the while loop ArrayResize(data,size+1);// Print("ArrayResize(data,size+1) - size + 1 = ", size + 1); cnt++; // cnt = 6 ? why not 5 ? Print("cnt++ - cnt = ", cnt); } }
The end result . . .
2012.10.15 09:11:46 sergery USDCHF,H1: size = 5 cnt = 4
2012.10.15 09:11:46 sergery USDCHF,H1: 1
2012.10.15 09:11:46 sergery USDCHF,H1: 2
2012.10.15 09:11:46 sergery USDCHF,H1: 3
2012.10.15 09:11:46 sergery USDCHF,H1: 4
2012.10.15 09:11:46 sergery USDCHF,H1: 5
Thank you very much !
// test_file_funcs.mq4 : indicator #property indicator_chart_window int init() { //---- //---- return(0); } int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { static bool once = false; // true only run one time if(once) return(0); //---- int i,handle; // write 5 numbers to file : file_write.dat handle = FileOpen("file_write.dat",FILE_BIN|FILE_WRITE); if(handle>0) { for(i=0;i<5;i++) FileWriteInteger(handle,i+1,LONG_VALUE); // 5 numbers:1,2,3,4,5 } FileClose(handle); // read these 5 numbers to an array : data[] handle = FileOpen("file_write.dat",FILE_BIN|FILE_READ); if(handle>0) { int cnt=0; // int dat,size,data[1]; dat = FileReadInteger(handle,LONG_VALUE); // read the first entry in the file while(FileIsEnding(handle)==false) { size = ArraySize(data); if(size>0) { data[size-1]=dat; // write the entry from the file into the array dat = FileReadInteger(handle,LONG_VALUE); // read the next entry in the file if(FileIsEnding(handle)) break; // if we have just read past the last element to the EOF quit the while loop ArrayResize(data,size+1);// cnt++; // } } } FileClose(handle); size = ArraySize(data); Print("size = ",size, " cnt = ",cnt);// size = 5 ,cnt = 4 for(i=0;i<size;i++) Print(data[i]); //---- once = true; // only run one time //---- return(0); } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use