How to write more than one line?

 

I'm still learning mql4 (one month experience :P) and i want my EA to write the current Day, TIme and Order, each time that the order changes. The first part was easy, but when my ea put some data in the csv file, it makes only one line and on each writting, this line is rewrited.

Example:

Today is day n° 117, the time now is 1303938780 and the ea is buying, then it writes: 117 1303938780 4
So, in our file is only written: 117 1303938780 4
And now, the day is still 117, but the time is 1403938780 and the ea is selling, then it writes: 117 1403938780 8

Then, in our file is only written: 117 1403938780 8, there's any other line, neither the last one.

This is my function:

bool Registrar(string arquivo,int ordem){
int FW,handle;
handle = FileOpen(arquivo,FILE_CSV|FILE_READ|FILE_WRITE," ");
if(handle < 1){
return(false);
}

FW = FileWrite(handle,DayOfYear(),Time[0],ordem);

if(FW < 1){
return(false);
}

FileFlush(handle);
FileClose(handle);
return(true);
}



Someone has an idea about what's the problem?
Thanks!

 

I don't have a lot of experience with file functions, but I believe that what you are doing at the moment is overwriting your file! You will need to find the end of the file first, then write your line. Try using the function:

bool FileSeek( int handle, int offset, int origin) 

That should enable you to add your information after the last line, rather than overwriting it....

 

int handle=FileOpen("filename.csv", FILE_CSV|FILE_READ|FILE_WRITE, ';');
if(handle>0)
{
FileSeek(handle, 0, SEEK_END);
//---- add data to the end of file
FileWrite(handle, data1, data2);
FileClose(handle);
handle=0;
}

From the reference.

 
Thaks guys, that solved my problem, but i have a doubt. If i need to go to the next line while reading, should i use FileSeek also?
 

No. When reading from a file the position is moved to "after the next thing you read" every time you read something from that file. This includes new lines.

So basically when reading, just read the file :) If you want to read a file and then go back to start of the file for example, you need to use FileSeek();

 
Thanks! it worked =)
Reason: