Write to File Syntax

To add comments, please log in or register
sd59
636
sd59  

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

Mehrdad Jeddi
29439
Mehrdad Jeddi  

Hi,

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

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

Regards.

sd59
636
sd59  
Mehrdad Jeddi:

Hi,

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

Regards.

Perfect! Thank you.

Mehrdad Jeddi
29439
Mehrdad Jeddi  
sd59:

Perfect! Thank you.

You're welcome.

William Roeder
20228
William Roeder  
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);
sd59
636
sd59  
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?

Keith Watford
Moderator
20477
Keith Watford  
sd59:

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

It was helpful and you should see it as such.

To add comments, please log in or register