Write File Operation

 

Hi guys

I thought it would be quite simple to achieve this but, it does not seem to be working for me.

I am trying to create my own log file so for example I want to write the current bid for every tick to a txt file.

The output i require is :

[Date] 12:24:53 The current bid is 1.33981

[Date] 12:24:57 The current bid is 1.33997

Thats all I require, heres the code I came up with but it doesnt work.

void LogOperations()
{
   int handle;
   string str = TimeDayOfYear(TimeCurrent()) + " The current Bid is " + Bid;
   handle=FileOpen("myLog.txt", FILE_BIN|FILE_WRITE);
    if(handle<1)
    {
     Print("can't open file error-",GetLastError());
     return(0);
    }
   FileWriteString(handle, str, 100);
   FileClose(handle);
}
 

try

FileOpen("myLog.txt", FILE_BIN|FILE_READ|FILE_WRITE);

 
qjol:

try

FileOpen("myLog.txt", FILE_BIN|FILE_READ|FILE_WRITE);

FileOpen("myLog.txt", FILE_CSV|FILE_READ|FILE_WRITE);

You are not writing a BINARY file. You want a txt file.

There is no need to keep opening and closing the file. That is very slow and you will need to do a FileSeek to the end of the file each time. Just open it once, and use FileFlush every now and then.

> it does not seem to be working for me

is not a very helpful fault report. This is what you would expect from a user, not from a programmer. If you can state the problem accurately it helps to find the solution :-)

 

Hi

Thanks for your replies, but i just want to write the below information to a text file for now, I am just working with some logging before I start planning out my EA.

So for example the output i require is :

[Date] 12:24:53 The current bid is 1.33981

[Date] 12:24:57 The current bid is 1.33997

Once I get this output, i can move on to phase 2 for my EA.

Can you please assist.

 
Blackberry:

Thanks for your replies, but i just want to write the below information to a text file for now, I am just working with some logging before I start planning out my EA.

Your code, in its current form, is going to log the latest price, constantly overwriting what was already in myLog.txt, and leaving only the most recent entry.

As dabbler has said, your description "it doesn't work" isn't very helpful. For example, you don't specify whether the problem is indeed that only the most recent entry is being recorded, or that you can't see the file at all. If the latter, then you may be misled about the cause of the problem. If MT4 is installed within the \Program Files area on Vista or later versions of Windows, then your output may be subject to UAC file virtualization. The file which you are trying to create within experts\files may be transparently redirected by the operating system to another area of disk. There's an endless number of topics on the forum about this issue, and how to solve it. As things stand, your code does create the file myLog.txt; it just doesn't log what you want.

Once you have located the file (if that is indeed part of the problem), then you need to do the following to fix the output so that it matches what you require:

1. As dabbler says, open the file with FILE_READ as well as FILE_WRITE. For example, handle=FileOpen("myLog.txt", FILE_BIN|FILE_READ|FILE_WRITE). The documentation (https://docs.mql4.com/files/FileOpen) says that files are truncated if they are opened with write access but not read access, and only the most recent entry would then be recorded.

2. After opening the file but before writing to it, move the pointer to the end of the file using FileSeek(handle, 0, SEEK_END). Otherwise, the new text will be written at the start of the file, overwriting its existing contents.

3. Change the length parameter for the call to FileWriteString(). Rather than using a fixed value of 100 you should instead be using StringLen(str).

4. Add newline characters (\r\n) to the string which you create. Otherwise, you are going to get a series of log entries without any separation between them.

5. You probably don't intend to use TimeDayOfYear(). This logs the number of days elapsed in the year so far, e.g. 59, leading to log entries such as "59 The current Bid is 1.07240000". Instead, you probably want to use TimeToStr().

 

Hi Guys

I would like to thank everybody above in helping me obtain the information I needed. JJC i will be more clearly in my posts next time and thank you for your help. I would just like to post the working method here so others can change it around and use it as they wish. My next step is to make this method more dynamic. But for now I have the info I want in the file.

PS can somebody please mark this thread as completed.

Thanks again

void LogOperations()
{
   int handle;
   string str = TimeToStr(TimeCurrent()) + " The current Bid is " + Bid + "\r\n";
   handle=FileOpen("myLog.txt", FILE_BIN|FILE_READ|FILE_WRITE);
    if(handle<1)
    {
     Print("can't open file error-",GetLastError());
     return(0);
    }
   FileSeek(handle, 0, SEEK_END);
   FileWriteString(handle, str, StringLen(str));
   FileClose(handle);
}