Has anybody come across square block characters when writing to a file using FileWrite() ?

 

When I write to a file using the FileWrite function, the text that appears in the file has square characters in it, typically around text that was passed to the FileWrite function as a variable. They are square blocks that are like what you would see when a application can't read a unicode character(). I have tried opening the file with notepad and wordpad and they come up in both applications.


This is how I open the file:


int day = TimeDay(TimeCurrent());

int month = TimeMonth(TimeCurrent());

int year = TimeYear(TimeCurrent());

string sFilename = StringConcatenate("Project123_",day,"-",month,"-",year,".txt");

file = FileOpen(sFilename,FILE_BIN|FILE_WRITE,'.');


And if I write to the file by calling:


FileWrite(file,"POSITION CLOSED, PROFIT/LOSS=",dProfit/10,"pips"); // dProfit is a double


it would print out:


'POSITION CLOSED, PROFIT/LOSS=▖12.3▖pips'


I have tried using a variety of delimiters but that has no affect, I have tried calling DoubleToStr(dProfit/10,1) instead but it still includes the block characters, and it is not limited to doubles, if I pass a string variable to the function it will print out block characters too.


Anybody come across this before?

 
jameshartell:

When I write to a file using the FileWrite function, the text that appears in the file has square characters in it, typically around text that was passed to the FileWrite function as a variable. They are square blocks that are like what you would see when a application can't read a unicode character(). I have tried opening the file with notepad and wordpad and they come up in both applications.

What you're getting is (probably) zeroes. FileWrite() is intended for use with files in CSV mode. You're opening the file as FILE_BIN. Therefore, MT4 is presumably semi-logically defaulting to using zero as the delimiter. The documentation recommends that you instead use FileWriteString() etc when dealing with files in binary mode. For example:

string strLine = StringConcatenate("POSITION CLOSED, PROFIT/LOSS= ",dProfit/10," pips");
FileWriteString(file, strLine, StringLen(strLine));
 
jjc:

What you're getting is (probably) zeroes. FileWrite() is intended for use with files in CSV mode. You're opening the file as FILE_BIN. Therefore, MT4 is presumably semi-logically defaulting to using zero as the delimiter. The documentation recommends that you instead use FileWriteString() etc when dealing with files in binary mode. For example:

Thanks very much jjc - I should have looked at the documentation more thoroughly, I didn't see FileWriteString() - Thanks!

Reason: