FileWrite() Problems.

 

Can someone help me with a FileWrite() problem? Initially I have had problems with writing large amounts of data to a single file as something caused the data file to be overwritten at a certain interval. In other words data was saved to a txt file and when the record count approached 27,598,602 records, the file was overwritten and the records resumed in the txt file at 27,598,603. Thinking that this problem may have something to do with large files, I wrote a program to save smaller record counts in many more files. In other words the EA was coded to write 20,000,000 records to a file and when this record count was triggered, a new file was started with record count 20,000,001. What I have noted is that the multiple data files continue to be overwritten with smaller record counts. Though my code is not perfect (ie. close the file after each write), I still have not solved the inherent problem of a file being overwritten. Does anyone know why this is occuring?

https://www.mql5.com/en/forum/137736

//+------------------------------------------------------------------+
//|                                              CheckModeling#6.mq4 |
//|                                               mcertini@gmail.com |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

extern string FileName1 = "EURUSD#1.txt";
extern string FileName2 = "EURUSD#2.txt";
extern string FileName3 = "EURUSD#3.txt";
extern string FileName4 = "EURUSD#4.txt";

extern string TextToAppend1 = "";
extern string TextToAppend2 = "";

int count;

int start()
   {
   string Yr = Year();
      
   string num1 = DoubleToStr(Month(),0);
   int Length1 = StringLen(num1);
   
   if(Length1==1)
      {
      string Mon = StringConcatenate("0",Month());
      }
      else
      {
      Mon = Month();
      }
     
   string num2 = DoubleToStr(Day(),0);
   int Length2 = StringLen(num2);
   
   if(Length2==1)
      {
      string Date = StringConcatenate("0",Day());
      }
      else
      {
      Date = Day();
      }
      
   string num3 = DoubleToStr(Hour(),0);
   int Length3 = StringLen(num3);
   
   if(Length3==1)
      {
      string Hr = StringConcatenate("0",Hour());
      }
      else
      {
      Hr = Hour();
      }
      
      
   string num4 = DoubleToStr(Minute(),0);
   int Length4 = StringLen(num4);
   
   if(Length4==1)
      {
      string Min = StringConcatenate("0",Minute());
      }
      else
      {
      Min = Minute();
      }

   string concat = StringConcatenate(Yr,Mon,Date,Hr,Min);
   string Open_0    = DoubleToStr(Open[0],5);
   string High_0    = DoubleToStr(High[0],5);
   string Low_0     = DoubleToStr(Low[0],5);
   string Close_0   = DoubleToStr(Close[0],5);
   string Volume_0  = DoubleToStr(Volume[0],5);
   string Bid_0     = DoubleToStr(Bid,5);
   string Open_1    = DoubleToStr(iOpen(NULL,PERIOD_M1,0),5);
   string High_1    = DoubleToStr(iHigh(NULL,PERIOD_M1,0),5);
   string Low_1     = DoubleToStr(iLow(NULL,PERIOD_M1,0),5);
   string Close_1   = DoubleToStr(iClose(NULL,PERIOD_M1,0),5);
   string Volume_1  = DoubleToStr(iVolume(NULL,PERIOD_M1,0),5);
   string Open_5    = DoubleToStr(iOpen(NULL,PERIOD_M5,0),5);
   string High_5    = DoubleToStr(iHigh(NULL,PERIOD_M5,0),5);
   string Low_5     = DoubleToStr(iLow(NULL,PERIOD_M5,0),5);
   string Close_5   = DoubleToStr(iClose(NULL,PERIOD_M5,0),5);
   string Volume_5  = DoubleToStr(iVolume(NULL,PERIOD_M5,0),5);
   string Open_15   = DoubleToStr(iOpen(NULL,PERIOD_M15,0),5);
   string High_15   = DoubleToStr(iHigh(NULL,PERIOD_M15,0),5);
   string Low_15    = DoubleToStr(iLow(NULL,PERIOD_M15,0),5);
   string Close_15  = DoubleToStr(iClose(NULL,PERIOD_M15,0),5);
   string Volume_15 = DoubleToStr(iVolume(NULL,PERIOD_M15,0),5);
   string Open_30   = DoubleToStr(iOpen(NULL,PERIOD_M30,0),5);
   string High_30   = DoubleToStr(iHigh(NULL,PERIOD_M30,0),5);
   string Low_30    = DoubleToStr(iLow(NULL,PERIOD_M30,0),5);
   string Close_30  = DoubleToStr(iClose(NULL,PERIOD_M30,0),5);
   string Volume_30 = DoubleToStr(iVolume(NULL,PERIOD_M30,0),5);
   string Open_60   = DoubleToStr(iOpen(NULL,PERIOD_H1,0),5);
   string High_60   = DoubleToStr(iHigh(NULL,PERIOD_H1,0),5);
   string Low_60    = DoubleToStr(iLow(NULL,PERIOD_H1,0),5);
   string Close_60  = DoubleToStr(iClose(NULL,PERIOD_H1,0),5);
   string Volume_60 = DoubleToStr(iVolume(NULL,PERIOD_H1,0),5);
   string Open_240  = DoubleToStr(iOpen(NULL,PERIOD_H4,0),5);
   string High_240  = DoubleToStr(iHigh(NULL,PERIOD_H4,0),5);
   string Low_240   = DoubleToStr(iLow(NULL,PERIOD_H4,0),5);
   string Close_240 = DoubleToStr(iClose(NULL,PERIOD_H4,0),5);
   string Volume_240= DoubleToStr(iVolume(NULL,PERIOD_H4,0),5);
   string Open_1440 = DoubleToStr(iOpen(NULL,PERIOD_D1,0),5);
   string High_1440 = DoubleToStr(iHigh(NULL,PERIOD_D1,0),5);
   string Low_1440  = DoubleToStr(iLow(NULL,PERIOD_D1,0),5);
   string Close_1440= DoubleToStr(iClose(NULL,PERIOD_D1,0),5);
   string Volume_1440= DoubleToStr(iVolume(NULL,PERIOD_D1,0),5);
   string Open_10880 = DoubleToStr(iOpen(NULL,PERIOD_W1,0),5);
   string High_10880 = DoubleToStr(iHigh(NULL,PERIOD_W1,0),5);
   string Low_10880  = DoubleToStr(iLow(NULL,PERIOD_W1,0),5);
   string Close_10880= DoubleToStr(iClose(NULL,PERIOD_W1,0),5);
   string Volume_10880= DoubleToStr(iVolume(NULL,PERIOD_W1,0),5);
   string Open_43200 = DoubleToStr(iOpen(NULL,PERIOD_MN1,0),5);
   string High_43200 = DoubleToStr(iHigh(NULL,PERIOD_MN1,0),5);
   string Low_43200  = DoubleToStr(iLow(NULL,PERIOD_MN1,0),5);
   string Close_43200= DoubleToStr(iClose(NULL,PERIOD_MN1,0),5);
   string Volume_43200= DoubleToStr(iVolume(NULL,PERIOD_MN1,0),5);
 
   TextToAppend1 = StringConcatenate(count,";",concat,";",Yr,";",Mon,";",Date,";",Hr,";",Min,";",Open_0,";",High_0,";",Low_0,";",Close_0,";",Volume_0,";",Open_1,";",High_1,";",Low_1,";",Close_1,";",Volume_1,";",Open_5,";",High_5,";",Low_5,";",Close_5,";",Volume_5,";",Open_15,";",High_15,";",Low_15,";",Close_15,";",Volume_15,";",Open_30,";",High_30,";",Low_30,";",Close_30,";",Volume_30);
   TextToAppend2 = StringConcatenate(TextToAppend1,";",Open_60,";",High_60,";",Low_60,";",Close_60,";",Volume_60,";",Open_240,";",High_240,";",Low_240,";",Close_240,";",Volume_240,";",Open_1440,";",High_1440,";",Low_1440,";",Close_1440,";",Volume_1440,";",Open_10880,";",High_10880,";",Low_10880,";",Close_10880,";",Volume_10880,";",Open_43200,";",High_43200,";",Low_43200,";",Close_43200,";",Volume_43200);
   
   //Print("Pre-Count","***",count);
      
   if(count <= 20000000)
      {
      FileAppend1(FileName1,TextToAppend2);
      Print(FileName1," ",count);
      }
      
   if(count > 20000000 && count <= 40000000)
      {
      FileAppend2(FileName2,TextToAppend2);
      Print(FileName2," ",count);
      }
   
   if(count > 40000000 && count <= 60000000)
      {
      FileAppend3(FileName3,TextToAppend2);
      Print(FileName3," ",count);
      } 
   
   if(count > 60000000 && count <= 80000000)
      {
      FileAppend4(FileName4,TextToAppend2);
      Print(FileName4," ",count);
      }     
   if(count > 80000000)
      {
      return(0);
      }
   
   count = count + 1;
   return(0);   
   }
   
void FileAppend1(string name, string txt)
   {
   int handle1 = FileOpen(name,FILE_CSV|FILE_READ|FILE_WRITE);
   FileSeek(handle1,0,SEEK_END);
   FileWrite(handle1,txt);
   FileFlush(handle1);
   FileClose(handle1);   
   }
   
void FileAppend2(string name, string txt)
   {
   int handle2 = FileOpen(name,FILE_CSV|FILE_READ|FILE_WRITE);
   FileSeek(handle2,0,SEEK_END);
   FileWrite(handle2,txt);
   FileFlush(handle2);
   FileClose(handle2);   
   }
   
void FileAppend3(string name, string txt)
   {
   int handle3 = FileOpen(name,FILE_CSV|FILE_READ|FILE_WRITE);
   FileSeek(handle3,0,SEEK_END);
   FileWrite(handle3,txt);
   FileFlush(handle3);
   FileClose(handle3);   
   } 
   
void FileAppend4(string name, string txt)
   {
   int handle4 = FileOpen(name,FILE_CSV|FILE_READ|FILE_WRITE);
   FileSeek(handle4,0,SEEK_END);
   FileWrite(handle4,txt);
   FileFlush(handle4);
   FileClose(handle4);   
   }     
Files:
 

I don't know what the problem is but there is clearly no need for separate FileAppend functions. You are passing a new file name at the same time. Just one FileAppend function and using the unique names you have defined should be more than enough. Also the FileFlush immediately before the FileClose is redundant. The documentation explicitly says so.

I bet that if you remove the FILE_READ and the FILE_SEEK, leave the file open (but FileFlush after each write) it will just "magically" work.

Reason: