File Reading Problem

 

I am having trouble with sequential calls to a function that opens a file, reads from the file, closes the file, and returns the retrieved data.


The first function call works, no problem. However, the second function call causes the system to freeze. I have to force close MT4 and restart it.


Each line of the file has two pieces of data, comma separated of course.


The function loops through each line of the file, storing the first and second data items of each line in variables, then comparing the first data item to the function input parameter. When a match is found, the function breaks out of the loop, closes the file, and returns the second data item from the line that was read last. Works great as long as I only call the function once.


Of course, that's the problem. I need to retrieve multiple values from the file via multiple function calls.


Thanks in advance for the help.

double RetrieveData(string InputData)
{
    double OutputData;
    int FileHandle;
    string FileName;
    string FileData;
	
    FileName = "Data.csv";
    FileHandle = FileOpen(FileName,FILE_CSV|FILE_READ,',');
    if(FileHandle > 0)
    {
        for(int i = 1; i <= 37; i++)
        {
	    FileData = FileReadString(FileHandle);
	    OutputData = FileReadNumber(FileHandle);
	    if(FileData == InputData) break;
	}
	FileClose(FileHandle);
    }
    return(OutputData);
}
 

have looksee below you may spot your issues...

mainly check for EOF, is only mod made to your ()


//+------------------------------------------------------------------+
//|                                                        29597.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#include <stdlib.mqh>


double RetrieveData(string InputData)
{
  double OutputData;
  int FileHandle;
  string FileName;
  string FileData;

  FileName = "Data.csv";
  FileHandle = FileOpen(FileName,FILE_CSV|FILE_READ,',');
  if(FileHandle > 0)
  {
    //for(int i = 1; i <= 37; i++)
    for(int i = 1; !FileIsEnding(FileHandle) && i <= 37; i++)
    {
      FileData = FileReadString(FileHandle);
      OutputData = FileReadNumber(FileHandle);
      if(FileData == InputData) break;
    }
    FileClose(FileHandle);
  }
  return(OutputData);
}

//what happens if error?
//  return error code
//what happens at present if caller gets 'something' due to error in called ()?


//-----------------------------------------------------------------------------
//
int start()
{
  string sKey;
  int iFh = FileOpen("Data.csv",FILE_CSV|FILE_WRITE,',');
  
  if(iFh>=0)
  {
    //load file with test data
    for(int i=1;i<=37;i++) FileWrite(iFh,StringConcatenate("Key.",i),i+0.1);
    FileClose(iFh);
    
    //let's test function...
    for(i=37;i>=1;i--)
    {
      sKey = StringConcatenate("Key.",i);
      Print(sKey," = ",RetrieveData(sKey));
    }
  }
  else
    Print("OpenError on \"Data.csv\" Error = ",ErrorDescription(GetLastError()));
  return(0);
}
Data.csv

Key.1,1.1
Key.2,2.1
Key.3,3.1
Key.4,4.1
Key.5,5.1
Key.6,6.1
Key.7,7.1
Key.8,8.1
Key.9,9.1
Key.10,10.1
Key.11,11.1
Key.12,12.1
Key.13,13.1
Key.14,14.1
Key.15,15.1
Key.16,16.1
Key.17,17.1
Key.18,18.1
Key.19,19.1
Key.20,20.1
Key.21,21.1
Key.22,22.1
Key.23,23.1
Key.24,24.1
Key.25,25.1
Key.26,26.1
Key.27,27.1
Key.28,28.1
Key.29,29.1
Key.30,30.1
Key.31,31.1
Key.32,32.1
Key.33,33.1
Key.34,34.1
Key.35,35.1
Key.36,36.1
Key.37,37.1
Expert Log

2010.02.08 19:39:32	29597 EURUSD,H1: uninit reason 0
2010.02.08 19:39:32	29597 EURUSD,H1: Key.1 = 1.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.2 = 2.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.3 = 3.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.4 = 4.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.5 = 5.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.6 = 6.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.7 = 7.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.8 = 8.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.9 = 9.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.10 = 10.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.11 = 11.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.12 = 12.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.13 = 13.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.14 = 14.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.15 = 15.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.16 = 16.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.17 = 17.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.18 = 18.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.19 = 19.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.20 = 20.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.21 = 21.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.22 = 22.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.23 = 23.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.24 = 24.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.25 = 25.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.26 = 26.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.27 = 27.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.28 = 28.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.29 = 29.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.30 = 30.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.31 = 31.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.32 = 32.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.33 = 33.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.34 = 34.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.35 = 35.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.36 = 36.1
2010.02.08 19:39:32	29597 EURUSD,H1: Key.37 = 37.1
2010.02.08 19:39:32	29597 EURUSD,H1: loaded successfully
 
fbj:

have looksee below you may spot your issues...

mainly check for EOF, is only mod made to your ()


Thanks, fbj, for your help.


I ran your script successfully on my computer. Then I modified my code as you suggested but still had the same problem.


Hmmm....it had to be the CSV file was the problem.


As it turns out, I had no "\r\n" at the end of the last line of the file (I edited it out, not knowing it would cause a problem).

Apparently, this screwed up the FileClose function and caused the system to hang.


Replacing the "\r\n" (via a carriage return) solved the problem. However, I would never have figured it out without your script.


Thanks again!

 

Well there ya go! The power of the quicky test harness strikes again :-)

Best to you...


Reason: