Here are a couple of functions I've written for you.
CB
//+------------------------------------------------------------------+ bool fnReadFile() { iHandle = FileOpen(sFileName,FILE_CSV|FILE_READ); if(iHandle < 1) { iErrorCode = GetLastError(); if (iErrorCode == 4103) Print("File not found"); else Print("Error reading file: ",iErrorCode); return(false); } iInteger = StrToInteger(FileReadString(iHandle)); sString = FileReadString(iHandle); dDouble = StrToDouble(FileReadString(iHandle)); FileClose(iHandle); return(true); } //+------------------------------------------------------------------+ bool fnWriteFile() { iHandle = FileOpen(sFileName,FILE_CSV|FILE_WRITE); if(iHandle < 1) { iErrorCode = GetLastError(); Print("Error updating file: ",iErrorCode); return(false); } FileWrite(iHandle,iInteger,sString,DoubleToStr(dDouble,iDecimalPlaces)); FileClose(iHandle); return(true); } //+------------------------------------------------------------------+
if(iHandle < 1)
A piece of almost unbelievable pedantry...
The documentation doesn't say this (https://docs.mql4.com/files/FileOpen). It says that the return value is -1 if the function fails. According to the documentation, -25356 could be a valid file handle.
Moving from pedantry to real life, I would actually be a tiny bit cautious about this. FileOpen() in MQ4 is not a simple wrapper around the CreateFile() function in the Win32 API. However, the return value from CreateFile() is INVALID_HANDLE_VALUE if the operation fails. INVALID_HANDLE_VALUE is -1; all other values such as -25356 can be potentially valid file handles - and very occasionally are. Therefore, depending on what MT4 is doing internally, negative signed integers could potentially represent valid file handles.
Here are a couple of functions I've written for you.
CB
This helps. Thank you so much!
I want to append to an existing .CSV file. I tried FILE_CSV|FILE_WRITE|FILE_READ but it didn't work. anyone done that yet?
I want to append to an existing .CSV file. I tried FILE_CSV|FILE_WRITE|FILE_READ but it didn't work. anyone done that yet?
Yes, that's exactly how to do it. It might not work for u if that file is already opened by another process in a mode that does not allow file sharing. So please check that the file is not opened by another process (either in metatrader or any other windows program). You can also check experts/journal log for any specific errors...
Hello I have using this code:
void WriteCSV() {
int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
FILE_CSV|FILE_READ|FILE_WRITE, ','
);
if ( handle > 0 ) {
FileSeek( handle, 0, SEEK_END );
FileWrite( handle,
TimeToStr( TimeCurrent(), TIME_DATE )
);
FileWrite( handle,
"hello" );
FileClose( handle );
}
The second FileWrite writes in the next row. How could I do it to make it write in the next row? In excel would be B1 instead of A2?
THank you
BeLikeWater
Hello I have using this code:
void WriteCSV() {
int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
FILE_CSV|FILE_READ|FILE_WRITE, ','
);
if ( handle > 0 ) {
FileSeek( handle, 0, SEEK_END );
FileWrite( handle,
TimeToStr( TimeCurrent(), TIME_DATE )
);
FileWrite( handle,
"hello" );
FileClose( handle );
}
The second FileWrite writes in the next row. How could I do it to make it write in the next row? In excel would be B1 instead of A2?
THank you
BeLikeWater
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I can write files to say a .dat file, but I'm looking to write to .csv, and to sort data into different columns and cells.
I'm also looking to be able to retrieve "individual cell" data through my EA.
Can anyone help, point me in a direction, or provide a sample for me?
Thank you!