FILE_READ | FILE_WRITE Question?

 

I was working with FileHistory and opened file with FILE_READ|FILE_WRITE combination.

It appears that at first writing to the file file gets extended, but as soon as FileRead... is made extending file becomes impossible, that is when writing to the end of the file 0 bytes gets written and last error is zero too. In other words:

      ExtHandle=FileOpenHistory(Symbol()+"T"+i_period+".hst", FILE_BIN|FILE_READ|FILE_WRITE); // open read/write
    FileSeek(ExtHandle,0,SEEK_END); // go to end of file
    int byteswritten=FileWriteInteger(ExtHandle,i_time,LONG_VALUE); // write integer to end of file ok
    Print("bytes written=",byteswritten," err=",GetLastError()); // byteswritten=4 err=0
    FileSeek(ExtHandle,-4,SEEK_END); // go to value just written
    int x=FileReadInteger(ExtHandle,i_time,LONG_VALUE); // read value just written
    byteswritten=FileWriteInteger(ExtHandle,i_time,LONG_VALUE); // write integer again to end of file
    Print("bytes written=",byteswritten," err=",GetLastError()); // byteswritten=0 err=0
I don't know is it MQ4 bug or just the way windows files work?
 

You may need to use FileFlush() to complete write operations.

Newly "written" data doesn't always go to the physical disk right away, unless the
cache is flushed or the file closed.

 
this
phy:

You may need to use FileFlush() to complete write operations.

Newly "written" data doesn't always go to the physical disk right away, unless the
cache is flushed or the file closed.


this has nothing to do with physical write, and I do have a flush
 

uh-huh

have a nice day...

 
Are you sure about -4? Shouldn't it be 4?
 
Try this scrypt

//+------------------------------------------------------------------+
//|                                                CheckFileSeek.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        https://www.metaquotes.net/ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net/ru/"
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
    int ExtHandle=FileOpenHistory(Symbol()+Period()+".hst", FILE_BIN|FILE_READ|FILE_WRITE); // open read/write
    //FileSeek(ExtHandle,0,SEEK_END); // go to end of file
    int i_time,byteswritten;//=FileWriteInteger(ExtHandle,i_time,LONG_VALUE); // write integer to end of file ok
    //Print("bytes written=",byteswritten," err=",GetLastError()); // byteswritten=4 err=0
    bool resFileSeek=FileSeek(ExtHandle,-4,SEEK_END); // go to value just written
    if (!resFileSeek) Print("FileSeek() is failed");
    int x=FileReadInteger(ExtHandle,LONG_VALUE); // read value just written
    Print("x written=",x," err=",GetLastError()); // 
    if (ExtHandle>0) FileClose(ExtHandle);
//----
   return(0);
  }
//+------------------------------------------------------------------+

Result:
2007.10.23 13:47:43 CheckFileSeek EURUSD,H1: removed
2007.10.23 13:47:43 CheckFileSeek EURUSD,H1: uninit reason 0
2007.10.23 13:47:43 CheckFileSeek EURUSD,H1: x written=1081757696 err=0
2007.10.23 13:47:43 CheckFileSeek EURUSD,H1: loaded successfully
2007.10.23 13:47:28 Compiling 'CheckFileSeek'
 

my point was that, I guess, even though you open file read/write, you can't read AND write to the file. As soon as I did read operation on the file I couldn't extend file anymore.

The script above only reads from the file.

 

phy was right. Read documentation about FileFlush function

Notes: The FileFlush() function must be called between operations of file reading and writing in the file.

 
Ah-ha here it is, sorry didn't understand what he was getting at.
 
stringo wrote >>

phy was right. Read documentation about FileFlush function

Notes: The FileFlush() function must be called between operations of file reading and writing in the file.

I have a similar problem with reading one and writing to another file. I tried FileFlush() after, but it did not correct the problem.

string delimiter = ","; // comma as delimiter

int handleA = FileOpen(readFile, FILE_CSV|FILE_READ,delimiter); // reading from file

//FileFlush(handleA); // this did not change anything

int handleB = FileOpen(writeFile, FILE_CSV|FILE_WRITE,delimiter); // writing to file, also tried FILE_CSV|FILE_READ|FILE_WRITE as suggested somewhere

....

while(!FileIsEnding(handleA)){
FileWriteString(handleB,FileReadString(handleA,1),1);
}

I am trying to copy readFile to writeFile, but all it creates is an EMPTY (writeFile) file. ReadFile contains 4 values in unformatted fields of CSV file, delimeter is ok (checked with notepad). Iget a "Incompatible access to a file" error wtih this code. I think reading is ok (tested with print command in while loop)), but writing seems to cause this problem.

Tried also do the same thing with int res = ShellExecuteA(0,0,"ren readFile writeFile,0,0,SW_SHOW); //for WinXP OS, but could not find a working solution with a proper command.

Can someone see the problem?

nickel

 

"also tried FILE_CSV|FILE_READ|FILE_WRITE as suggested somewhere"

That would be the correct opening method to append an existing file.

FileReadString(handleA,1)

Why a 1?

FileWriteString(handleB,......

FileWriteString is for writing to Binary file, not CSV

I suggest you review the documentation and samples for File operations.

Reason: