Working with files. - page 3

 
Yedelkin:
"When you close a file, the data is automatically reset to disk, so there is no need to call FileFlush() before calling FileClose()" - Yes, yes, I'm starting to see whatsergeev was talking about. So, it turns out that instead of FileClose() you can call FileFlush() to guarantee saving the last record to the file? And this would be a smart solution?

not instead of, but by necessity.

Flush - resets the remaining data, and does NOT close the file. This is what you want, right?

Close- resets the rest of the data to disk and closes.

 
sergeev:

not instead of, but by necessity.

Flush - resets the remaining data, and does NOT close the file. This is what you want, right?

Close - flushes the remaining data to disk and closes.

Yes, that's exactly what we're talking about. Thanks for the tip on a smart solution!
 
Yedelkin:

Something about time minimization using FileFlush() doesn't work very well:

2011.05.29 21:58:20 FlushSave (EURGBP,M1) FileFlush. GetTickCount() = 133766
2011.05.29 22:00:33 FlushSave (EURGBP,M1) FileClose. GetTickCount() = 133734
In fact, it takes the same amount of time for both functions to work.

As I understood this line moves position to the beginning of file without offset. This allows to overwrite existing information (i.e. the date is updated, but it does not accumulate in the file).

FileSeek(handle_file,0,SEEK_SET);

By using move to end of file instead of SEEK_SET, the data would pile up in the file.

 
Yedelkin:

You open a new file handle every time you flush. What for? And you don't close it by the way.

The advantage of the FileFlush function is that you don't need to reopen the handle.

Документация по MQL5: Файловые операции / FileFlush
Документация по MQL5: Файловые операции / FileFlush
  • www.mql5.com
Файловые операции / FileFlush - Документация по MQL5
 
Interesting:

1. As I understand it, this line moves the position in the file without offset. This allows existing information to be overwritten (i.e. the date is updated but does not accumulate in the file)

Thus, if used instead of SEEK_SET skip to the end of file, data will be accumulated in the file.
Already had time to delete my message. There in the example inserted FileFlush() after writing to the file.
 
TheXpert:

You open a new file handle every time you flush. What for? And you don't close it by the way.

The advantage of the FileFlush function is that you don't need to reopen the handle.

I did it like this:

int handle_file;
datetime t;
uint u;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---пример с функцией FileFlush()
   u=GetTickCount();
   handle_file=FileOpen("Ye_file.txt",FILE_READ|FILE_WRITE|FILE_TXT);
//for(int i=0;i<100000;i++)
   for(int i=0;i<1000;i++)
     {
      if(handle_file!=INVALID_HANDLE)
        {
         t=TimeLocal();
         FileSeek(handle_file,0,SEEK_SET);
         FileFlush(handle_file);
         FileWrite(handle_file,t);
        }
     }
   FileClose(handle_file);
   Print("FileFlush. GetTickCount() = ",GetTickCount()-u);

//---пример без функции FileFlush()
   u=GetTickCount();
   for(int i=0;i<1000;i++)
     {
      handle_file=FileOpen("Ye_file.txt",FILE_READ|FILE_WRITE|FILE_TXT);
      if(handle_file!=INVALID_HANDLE)
        {
         t=TimeLocal();
         FileSeek(handle_file,0,SEEK_SET);
         FileWrite(handle_file,t);
         FileClose(handle_file);
        }
     }
   Print("FileClose. GetTickCount() = ",GetTickCount()-u);
  }

Result:

2011.05.29 23:14:31 FlushSave (EURGBP,M1) FileFlush. GetTickCount() = 13563
2011.05.29 23:14:32 FlushSave (EURGBP,M1) FileClose. GetTickCount() = 531

Swapped the lines, according to documentation:

         FileFlush(handle_file);
         FileWrite(handle_file,t);
But I don't understand the point of calling FileFlush() before FileWrite().
 
Yedelkin:

I did it like this:

Result:

2011.05.29 23:14:31 FlushSave (EURGBP,M1) FileFlush. GetTickCount() = 13563
2011.05.29 23:14:32 FlushSave (EURGBP,M1) FileClose. GetTickCount() = 531

I swapped the lines, according to documentation:

But the point of calling FileFlush() before FileWrite() has yet to be understood.

Here is the variant:

int handle_file;
datetime t;
uint u;

void OnStart()
{
//---пример с функцией FileFlush()
u=GetTickCount();

handle_file=FileOpen("Ye_file.txt",FILE_READ|FILE_WRITE|FILE_TXT);

  for(int i=0;i<1000;i++)
  {
    if(handle_file!=INVALID_HANDLE)
    {
    t=TimeLocal();
    FileSeek(handle_file,0,SEEK_END);
    FileFlush(handle_file);
    FileWrite(handle_file,t);
    }
  }

FileClose(handle_file);

Print("FileFlush. GetTickCount() = ",GetTickCount()-u);

}

The result is FileFlush. GetTickCount() = 26125

Here is the variant:

int handle_file;
datetime t;
uint u;

void OnStart()
{
//---пример без функции FileFlush()
u=GetTickCount();

  for(int i=0;i<1000;i++)
  {
  handle_file=FileOpen("Ye_file2.txt",FILE_READ|FILE_WRITE|FILE_TXT);
      
    if(handle_file!=INVALID_HANDLE)
    {
    t=TimeLocal();
    FileSeek(handle_file,0,SEEK_END);
    FileWrite(handle_file,t);
    FileClose(handle_file);
    }

  }

Print("FileClose. GetTickCount() = ",GetTickCount()-u);

}
The result is FileClose. GetTickCount() = 3969
 

This option gave a result between 47 and 110

int handle_file;
datetime t;
uint u;

void OnStart()
{

u=GetTickCount();

handle_file=FileOpen("Ye_file.txt",FILE_READ|FILE_WRITE|FILE_TXT);

  for(int i=0;i<1000;i++)
  {
    if(handle_file!=INVALID_HANDLE)
    {
    t=TimeLocal();
    FileSeek(handle_file,0,SEEK_END);
    FileWrite(handle_file,t);
    }
  }

FileClose(handle_file);

Print("FileFlush. GetTickCount() = ",GetTickCount()-u);

}

1. Conclusion - Using FileFlush in a loop slows down execution by about 260 times.

2. A loop for 50,000 records in this variant has the following result - FileFlush. GetTickCount() = 1891.

3. I failed to kill the terminal when executing the 50000-write cycle without exiting the file (I closed the terminal and "killed" the process).

4. I was able to kill terminal with 100000 loop, and the file had more than 65536 records (so much space in Excel 2003).

 
Yedelkin:

I swapped the lines according to the documentation:

Where does it say so in documentation?

But I still do not understand the sense of calling FileFlush() before FileWrite().

How can you make sense of something that doesn't have one? Return the string order and double-check it. Apparently the documentation did not express it correctly.

But... Thanks to your tests, the bug seems to have been detected - FileFlush seems to eat up an inordinate amount of time when no changes are made.

Interesting:

OMG! It's where the inference is that it's a morass. This is how claims like "OOP is faster" or "indicators are slow, we should move the whole code to the Expert Advisor" appear.

 
papaklass:

Expert, write down how to use this function correctly.

Hypothetically so:

// open handle
for(int i=0;i<1000;i++)
  {
    if(handle_file!=INVALID_HANDLE)
    {
    t=TimeLocal();
    FileSeek(handle_file,0,SEEK_END);
    FileWrite(handle_file,t);
    FileFlush(handle_file);
    }
  }
// close handle

I.e. it is correct to compare FileClose -- FileOpen bundle with FileFlush.

Theoretically, FileFlush should be a part of FileClose and could not be slower than the bundle.

There is no point in flushing changes before they appear because they are not there yet :)

But, despite the wild conclusions, the tests are indicative, so we wait for comments from developers on how the function works when there are no changes.