Download MetaTrader 5
To add comments, please log in or register
Download MQL5 library codes via MetaTrader. It is convenient!
Fredrik Wenngren
199
Fredrik Wenngren 2010.03.27 12:24 
Hi,

Is this even possible or am I doomed to reading them by FileReadString ?

If it should be working then please look at my little test-code for this purpose below:

string FileName = "Readfromfile.csv";
double FileDouble;
int LastMinute;
int cnt;
int Handle;

int init () { return(0); }
int deinit () { return(0); }
int start () {

if (LastMinute == 59) { LastMinute = -1; } // MINUTE-COUNTER
if (LastMinute < Minute())
{
LastMinute = Minute();
cnt++;
FileFunc (cnt); // EVERY MINUTE CALL FILEFUNC ONCE
}
return(0); }

void FileFunc (int counter) {

Handle=FileOpen(FileName,FILE_CSV|FILE_READ,","); // OPEN FILE IN CSV & READ MODES
FileSeek ( Handle, counter, SEEK_SET ) ; // MOVE POINTER 1 STEP
FileDouble = FileReadDouble(Handle, DOUBLE_VALUE); // READ DOUBLE VALUE
Print("Count is "+cnt+", Value read from CSV is "+FileDouble); // This is where FileDouble always is 0.00000
FileClose (Handle); // but FileReadStr works in same code
}

The function basically moves the file-pointer by +1 every minute and not once before the end of file does it manage to read a double value.
If I instead change FileDouble variable to string and use FileReadString everything is working as expected but,
this would increase total string variable count in the code which is not wanted.

CSV File first 10 lines is shown here, rest is the same anyway:

#,TF,Time, GBP, USD,,,,,,
0,D1,2010.03.18 00:00, 51.28444, 45.51172, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
1,D1,2010.03.17 00:00, 52.74815, 44.06157, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
2,D1,2010.03.16 00:00, 48.34172, 48.44373, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
3,D1,2010.03.15 00:00, 38.77915, 58.12674, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
4,D1,2010.03.12 00:00, 46.75051, 50.03841, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
5,D1,2010.03.11 00:00, 39.85016, 57.03026, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
6,D1,2010.03.10 00:00, 35.59673, 61.40300, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
7,D1,2010.03.09 00:00, 36.49434, 60.47617, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
8,D1,2010.03.08 00:00, 39.22796, 57.66689, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
9,D1,2010.03.05 00:00, 43.53239, 53.28366, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
10,D1,2010.03.04 00:00, 37.81015, 59.12144, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000



I have read the docs and searched the forums but this specific problem is not mentioned, and I fear I have misunderstood something fundamental...

Thanks so much if someone could shed some light...

/ McKeen
Files:
gordon
2322
gordon 2010.03.27 14:26  
McKeen:
#,TF,Time, GBP, USD,,,,,,
0,D1,2010.03.18 00:00, 51.28444, 45.51172, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
1,D1,2010.03.17 00:00, 52.74815, 44.06157, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
2,D1,2010.03.16 00:00, 48.34172, 48.44373, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
3,D1,2010.03.15 00:00, 38.77915, 58.12674, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
4,D1,2010.03.12 00:00, 46.75051, 50.03841, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
5,D1,2010.03.11 00:00, 39.85016, 57.03026, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
6,D1,2010.03.10 00:00, 35.59673, 61.40300, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
7,D1,2010.03.09 00:00, 36.49434, 60.47617, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
8,D1,2010.03.08 00:00, 39.22796, 57.66689, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
9,D1,2010.03.05 00:00, 43.53239, 53.28366, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
10,D1,2010.03.04 00:00, 37.81015, 59.12144, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000

FileReadDouble() is for working with binary files... Your example is obviously a plain text file (in this case a CSV). It's irrelevant that some of the text represents a double, it's still text...


Info about binary and plain text formats: http://en.wikipedia.org/wiki/Binary_file, http://en.wikipedia.org/wiki/Plain_text,

Fredrik Wenngren
199
Fredrik Wenngren 2010.03.27 16:47  
gordon:

FileReadDouble() is for working with binary files... Your example is obviously a plain text file (in this case a CSV). It's irrelevant that some of the text represents a double, it's still text...


Info about binary and plain text formats: http://en.wikipedia.org/wiki/Binary_file, http://en.wikipedia.org/wiki/Plain_text,

Those links were valuable reads for me and it is now all clear.

Guess I was right about missing out on something fundamental =/

Thanks alot for taking the time!

Best Regards

/ McKeen
Benjamin Dixon
21209
Benjamin Dixon 2015.09.02 11:27  
For csv files, instead of:
FileReadDouble(....
Use this:
StringToDouble(FileReadString(... 

oops old thread but easy solution wasn't in it yet.
/
To add comments, please log in or register