Download MetaTrader 5

Read csv file with a single float number

To add comments, please log in or register
Manuel Sanchon
939
Manuel Sanchon  

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

honest_knave
Moderator
2326
honest_knave  

Is the file already open?

Manuel Sanchon
939
Manuel Sanchon  
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()
honest_knave
Moderator
2326
honest_knave  

Is the file in the correct folder?

if(!FileIsExist("test.csv")) Print("File not found!");
Manuel Sanchon
939
Manuel Sanchon  
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...
honest_knave
Moderator
2326
honest_knave  
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
Manuel Sanchon
939
Manuel Sanchon  

Thank you very much, it was done :)

honest_knave
Moderator
2326
honest_knave  

Glad to help

To add comments, please log in or register