Write to File Syntax

 

Please can someone spot the deliberate mistake!?

I want to write headers to a file in the first instance the subsequently write data. I check to see if file exists first - if it doesn't I write the headers, if it does I just continue to write the data.

When the file has been created it still goes into the first section as though the file does not exist - proven by the print statement.

Here is the code:

void WriteData(string filename)
{
 int handle = 0;
 //if the file does not exist create it and write the title headers
 if(!FileIsExist(filename,FILE_COMMON))
 {
  Print("file does not exist");
  handle = FileOpen(filename,FILE_WRITE|FILE_CSV,',');
  if(handle>0)
  {
   FileSeek(handle,0,SEEK_SET);
   FileWrite(handle,"header1","header2","header3");  
   FileWrite(handle,data1,data2,data3);         
  }else Alert(ErrorDescription(GetLastError()));
 
 }else    //if file does exist add data to it
      {
       Print("file exists");
       handle = FileOpen(filename,FILE_WRITE|FILE_CSV,',');
       if(handle>0)
       {
        FileSeek(handle,0,SEEK_END);
        FileWrite(handle,data1,data2,data3);
       }else Alert(ErrorDescription(GetLastError()));
      } 
  FileClose(handle); 
return;

thanks

 

Hi,

So you should open the file in a combination flag of the common folder :

FileOpen(filename,FILE_WRITE|FILE_CSV|FILE_COMMON,',');

Regards.

 
Mehrdad Jeddi:

Hi,

So you should open the file in a combination flag of the common folder :

Regards.

Perfect! Thank you.

 
sd59:

Perfect! Thank you.

You're welcome.

 
sd59: Please can someone spot the deliberate mistake!?
  1. Why are you deliberating making mistakes? That's one.

  2. You are checking for the file in common but creating it in MQL4/files.

  3.  handle = FileOpen(filename,FILE_WRITE|FILE_CSV,',');
    Whether the file exist or not you open it for writing. At that moment, it exists and is zero size. The if exists write else write is unnecessary.

  4. //if file does exist add data to it
    Whether the file exist or not, you open it for writing. At that moment, it exists and is zero size. The FileSeek is unnecessary.

  5. Whether the file exist or not, you open it for writing. At that moment, it exists and is zero size. You are not adding data, you are replacing the file completely.

  6. Why are you looking at common? Are you really using multiple terminal installations?
Just append whether it exists or not:
   int         APPEND   = FILE_CSV|FILE_WRITE|FILE_READ;
   string      name     =  …
   int      handle   = FileOpen(name, APPEND, '~');   if(handle < 1){
      Alert(&hellip;);  return;  }
   FileSeek(handle, 0, SEEK_END);
   FileWrite(handle, result);
   FileClose(handle);
 
William Roeder:
  1. Why are you deliberating making mistakes? That's one.

  2. You are checking for the file in common but creating it in MQL4/files.

  3. Whether the file exist or not you open it for writing. At that moment, it exists and is zero size. The if exists write else write is unnecessary.

  4. Whether the file exist or not, you open it for writing. At that moment, it exists and is zero size. The FileSeek is unnecessary.

  5. Whether the file exist or not, you open it for writing. At that moment, it exists and is zero size. You are not adding data, you are replacing the file completely.

  6. Why are you looking at common? Are you really using multiple terminal installations?
Just append whether it exists or not:

err.. was this meant to be helpful or just a rant?

 
sd59:

err.. was this meant to be helpful or just a rant?

It was helpful and you should see it as such.

Reason: