FileClose Problem.

 

Can someone help me with the included code? I am having problems with closing the first file and going onto the next file. What is occuring is that the initial file remains open. How do I close the first file when going onto the second file.

//+------------------------------------------------------------------+
//|                                              CheckModeling#8.mq4 |
//|                      Copyright © 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

extern string FileName1 = "EURUSD#1.txt";
extern string FileName2 = "EURUSD#2.txt";
extern string FileName3 = "EURUSD#3.txt";
extern string TextToAppend1 = "";
extern string TextToAppend2 = "";
int count1 = 0;
int count2 = 0;
int name1  = 0;

int counter = 0;

int handle1;
int handle2;

int deinit()
   {
   if(handle1 != 1)
      {
      FileClose(handle1);
      }
   }
   
int start()
   {
   FileAppend1(FileName1,count1);
   FileClose(handle1);
   FileAppend2(FileName2,count2);
   FileClose(handle2);

   }

void FileAppend1(string name1, int count1)
   {
   while(counter < 10)
      {
      handle1 = FileOpen(FileName1,FILE_CSV|FILE_READ|FILE_WRITE);
      if(handle1 == -1)
         {
         Print("File Print Failed");
         return(0);
         }
      
      if(handle1 != -1)
         {
         string Yr = Year();
   
         string num = DoubleToStr(Month(),0);
         int Length1 = StringLen(num);

         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_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);
 
         TextToAppend1 = StringConcatenate(counter,";",concat,";",Yr,";",Mon,";",Date,";",Hr,";",Min);
         TextToAppend2 = StringConcatenate(TextToAppend1,";",Open_60,";",High_60,";",Low_60);

         FileSeek(handle1,0,SEEK_END);
         FileWrite(handle1,TextToAppend2);
         FileFlush(handle1);
         counter++;
         Print("counter","***",counter);
         }
      }
   } 
   
void FileAppend2(string name1, int count2)
   {
   count2 = 10;
   while(count2 < 1000000)      
         {
         handle2 = FileOpen(FileName2,FILE_CSV|FILE_READ|FILE_WRITE);
         if(handle2 == -1)
            {
            Print("File Print Failed");
            return(0);
            }
      
         if(handle2 != -1)
            {
            string Yr = Year();
   
            string num = DoubleToStr(Month(),0);
            int Length1 = StringLen(num);

            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_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);

         
            TextToAppend1 = StringConcatenate(counter,";",concat,";",Yr,";",Mon,";",Date,";",Hr,";",Min);
            TextToAppend2 = StringConcatenate(TextToAppend1,";",Open_60,";",High_60,";",Low_60);

            FileSeek(handle2,0,SEEK_END);
            FileWrite(handle2,TextToAppend2);
            FileFlush(handle2);
            count2++;
            Print("count2","***",count2);
            }
         }   
   }   
         
Files:
 
ForexSurfr:

Can someone help me with the included code? I am having problems with closing the first file and going onto the next file. What is occuring is that the initial file remains open. How do I close the first file when going onto the second file.

You use FileClose
 
He is trying:
   FileAppend1(FileName1,count1);
   FileClose(handle1);
   FileAppend2(FileName2,count2);
   FileClose(handle2);
You are opening the file multiple times and loosing the earlier handles. Thus you open 10 and close 1 leaving 9 open.
   while(counter < 10)
      {
      handle1 = FileOpen(FileName1,FILE_CSV|FILE_READ|FILE_WRITE);
      if(handle1 == -1)..

Open the file once BEFORE the while, put the close AFTER the while, not in the caller.

Why are you writing the same values 10 times to the file?

 
WHRoeder:
He is trying:
You are opening the file multiple times and loosing the earlier handles. Thus you open 10 and close 1 leaving 9 open.

Open the file once BEFORE the while, put the close AFTER the while, not in the caller.

Why are you writing the same values 10 times to the file?


WHRoeder,

Thank you for your reply. This is just a prototype for the final code I am building. I am having problems writing large text files. When my record count approaches a certain amount (ie. 27,000,000 Records), for some reason, the file gets overwritten. Initially I thought that the internal counter in my code was maxing out for the variable that I was using. This is not so as I built another program and had no problem generating many more records in a continuous file. As a result, I am have to build multiple text files to get the complete population of records I want.

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

 

I made the change but continue to receive error messages after the program reaches record #41. After this I get the error messages:

FileOpen - too many opened files

EURUSD, M1: File Print Failed

Files:
 

Don't do this .. . .

 handle1 = FileOpen(FileName1,FILE_CSV|FILE_READ|FILE_WRITE);
   while(counter < 10)
      {
      handle1 = FileOpen(FileName1,FILE_CSV|FILE_READ|FILE_WRITE);
      if(handle1 == -1)
         {

do this . . .

 handle1 = FileOpen(FileName1,FILE_CSV|FILE_READ|FILE_WRITE);
   while(counter < 10)
      {
                                 //  <--- line deleted
      if(handle1 == -1)
         {
 
Once the file is open . . . it is open, you don't need to open it again unless you closed it first.
 

RaptorUK,

Thank you for your response. Dumb misstake on my part. I am getting lazy and am going to have to take more time in reviewing my code.

Everything is working great now.

Reason: