It is my first to use file in MQL4, help!

 

here is my code:

int start()
  {
   int    counted_bars=IndicatorCounted();
   int handle, _GetLastError;
   string File_Name="spreadandbar.csv";
   int time, count;
   double spread;
   datetime time_chk;
   
   handle=FileOpen(File_Name,FILE_CSV|FILE_READ|FILE_WRITE,";");
   
   if(handle<0) // File opening fails
   {
      if( GetLastError() == 4103 ) { Alert("No file named ",File_Name+", handle= "+handle); }
      else
      {
         Alert("error code= "+GetLastError());
         Alert("Error while opening file ["+File_Name+"], may be the file is busy by the other appliction, handle="+handle);
         PlaySound("Bzrrr.wav");
      }
      return;
   }
   else
   {
      time = iTime(Symbol(),1,0);
      spread = MarketInfo(Symbol(),MODE_SPREAD);
      count += 1;
   
      handle = FileOpen(File_Name, FILE_CSV|FILE_READ|FILE_WRITE);
   
      if ( !FileSeek ( handle, 0, SEEK_END ) )
      {
         _GetLastError = GetLastError();
         Print( "FileSeek ("+handle+", 0, SEEK_END ) - Error #", _GetLastError );
         return;
      }
  
      FileWrite(handle,count,time,spread);
      
      if( FileWrite(handle, spread) < 0 || FileWrite(handle, time) < 0 )
      {
         _GetLastError = GetLastError();
         Alert(Symbol()+", chart ("+Period()+"), filename is a_spread_and_bar, FileWrite() Error #", _GetLastError );
      }
   }

   FileClose(handle);

   return(0);
}

After attaching the indicator to a chart, "no file named" alert messaged is issued after some time.

However, when I check the folder, I can see and open the csv file. Would anybody help to tell what's wrong with my code?

Thanks a lot!

 
wing:

here is my code:

<SNIP>

After attaching the indicator to a chart, "no file named" alert messaged is issued after some time.

However, when I check the folder, I can see and open the csv file. Would anybody help to tell what's wrong with my code?

Thanks a lot!

Please edit your post . . .

Please use this to post code . . . it makes it easier to read.

 
RaptorUK:

Please edit your post . . .

Please use this to post code . . . it makes it easier to read.


done.
 
 handle=FileOpen(File_Name,FILE_CSV|FILE_READ|FILE_WRITE,";");
   
   if(handle<0) // File opening fails
   :
   else
   {
      handle = FileOpen(File_Name, FILE_CSV|FILE_READ|FILE_WRITE);
You open the file, then you open the file AGAIN, loosing the original handle. Eventually you run out of available handles and you error.
 
wing:

here is my code:

After attaching the indicator to a chart, "no file named" alert messaged is issued after some time.

However, when I check the folder, I can see and open the csv file. Would anybody help to tell what's wrong with my code?

Thanks a lot!


I think if you remove the second FileOpen statement from the code, your problem will be gone. When you come to your seconf fileopen statement, file is already opened successfully by the first statement.
 
Problem solved, thanks to you all!