Read csv file with a single float number

 

I need to read a .csv file with a single number (say 0.45365345). Depending of this number the expert will make decisions on trading...

I have wrote the following piece of code in the start() function:

// Read the file with the probability

      int handle=FileOpen("test.csv", FILE_CSV, ';');

      if(handle==INVALID_HANDLE)

        {

         Print(GetLastError());

         ResetLastError();

        }

      else

        {

         PrintFormat("File %s has been successfully opened");

         //--- The opened file is not needed any more, so close it

         double current_prob=(double)FileReadFloat(handle);

         FileClose(handle);

        }

However, I always obtain error code 5004. I was looking for more than one hour but every thing I try fails

I have been able to write files without problems...


I would appreciate any help. Thank you in advance

 

Is the file already open?

 
honest_knave:

Is the file already open?


Should be closed. I create the file with python:

f = open(path_data_new + "test.csv", "wb")
writer = csv.writer(f)
entries = [0.575673]
print(entries)
writer.writerow(entries)
f.close()
 

Is the file in the correct folder?

if(!FileIsExist("test.csv")) Print("File not found!");
 
honest_knave:

Is the file in the correct folder?


Yes, I've found that problem. I was checking everything with the tester and the file has to be in ~/tester/files instead of ~/MQL4/Files

Now, open the file but it returns 0 instead of 0.575673 for current_prob... I'll give a look.

Thank you

P.D. Now, it's OK. I've changed FileReadFloat by FileReadNumber...
 
Manuel Sanchon:

Yes, I've found that problem. I was checking everything with the tester and the file has to be in ~/tester/files instead of ~/MQL4/Files

Now, open the file but it returns 0 instead of 0.575673 for current_prob... I'll give a look.

Thank you

FileReadFloat() is for binary files. Try FileReadNumber() instead.
FileReadNumber - File Functions - MQL4 Reference
FileReadNumber - File Functions - MQL4 Reference
  • docs.mql4.com
FileReadNumber - File Functions - MQL4 Reference
 

Thank you very much, it was done :)

 

Glad to help

Reason: