array and file problem

 
hi all, this is part of my code
int handle1,handle2,write;
string File_Name="mydata.csv";
double Erray[1,0];
Erray[0,0]=pastfractalup;
Erray[1,0]=pastfractaldown;
{
   if (handle1<1)
      {
      handle1=FileOpen(File_Name,FILE_CSV|FILE_WRITE,";");// File opening
      }
   if (fractalup>0) 
      {
      write=FileWriteDouble(handle1,Erray[0,0],DOUBLE_VALUE);
      }
   else if (fractaldown>0) 
      {
      write=FileWriteDouble(handle1,Erray[1,0],DOUBLE_VALUE);
      }
   FileClose(handle1);
}   
{
   if (handle2<1)
      {
      handle2=FileOpen(File_Name,FILE_CSV|FILE_READ,";");// File opening
      }
   FileReadArray(handle2,Erray, 0, 2);
   FileClose(handle2);   
}

 question are

1,is there any thing wrong with the code?

 2.in the file_read section, what is the value read out, how to use it?

3,i should have 2 data,erray 0,0 and erray 1,0 ,how to show it out by comment?

 
double Erray[2,1];// [1,0];
Erray[0,0]=pastfractalup;
Erray[1,0]=pastfractaldown;
an array[2] has index 0 and 1
 
i had change it, but now the problem is, it only attempt to read or write the file, why?
and when it write the data in the file become blank.
 
shenlongming:
i had change it, but now the problem is, it only attempt to read or write the file, why?
and when it write the data in the file become blank.
handle1=FileOpen(File_Name,FILE_CSV|FILE_WRITE,";");// File opening
write=FileWriteDouble(handle1,Erray[0,0],DOUBLE_VALUE);
FileReadArray(handle2,Erray, 0, 2);

You are creating a TEXT file (CSV) and you are using a BINARY write/read functions.

Also the third parameter to FileOpen is an int not a string. RTFM

 

can you guide me further?

how do i change the binary write to something else?i didnt see any tutorial about that,

Thx a lot. 

 

i had successfully writing the data to the file

int handle1;                               // File descriptor
string File_Name="mydata1.csv";                          // File name
string Erray[2,1];                                       // Array 
string read;
Erray[0,0]=fractalup;
Erray[1,0]=fractaldown;
   {
   handle1=FileOpen(File_Name,FILE_CSV|FILE_WRITE,";");  //File opening
   
   if(fractalup>0)
      {
      FileWrite(handle1,Erray[0,0]);      //Writing to the file
         if(write < 0)                          // If failed
            {
            FileClose(handle1);                // File closing
            return;                             // Exit start()      
            }
      }
      
   Alert("The ",File_Name," file updated.");    // Message
   FileClose(handle1);
   }

 now my question is, why the data is not stored in the file?
when fractalup>0, it wrote the value of fractalup,
but then when fractalup=0, the data in the file also became 0, i wonder why?i had no other code related to this one, i dont think it will write data to the file, but why it still gone blank?

 

You posting an incomplete code. What is the value of write ?

FileWrite(handle1,Erray[0,0]);      //Writing to the file
if (write < 0)                         // If failed

The reason why when fractalup = 0 the data is also 0 is because you using the the FILE_WRITE without FILE_READ. That way, the file operation overwrite/delete the previous data. Try to use

handle1=FileOpen(File_Name,FILE_CSV|FILE_WRITE|FILE_READ,";");  //File opening

I wrote this working script below base on your first code using binary file, but I thought WHRoeder's answer is sufficient enough so I didn't post it

#include <stdlib.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {   
  PlaySound ("wait.wav");
  
  int handle1, handle2, write;
  string File_Name = "mydata.bin";
  double Erray1[2,2], Erray2[2,2];
     Alert ("Declaring array ",ErrorDescription(GetLastError()));

  Erray1[0,0] = 1.23456;
     Alert ("Initialising array 0 0 ",ErrorDescription(GetLastError()));
  Erray1[1,0] = 2.34567;
     Alert ("Initialising array 1 0 ",ErrorDescription(GetLastError()));
  Erray1[0,1] = 3.45678;
     Alert ("Initialising array 0 1 ",ErrorDescription(GetLastError()));
  Erray1[1,1] = 4.56789;
     Alert ("Initialising array 1 1 ",ErrorDescription(GetLastError()));
  
  handle1 = FileOpen(File_Name,FILE_BIN|FILE_WRITE);// File opening
     Alert ("Opening handle 1 ",handle1," ",ErrorDescription(GetLastError()));
  write = FileWriteArray(handle1,Erray1, 0, 4);
     Alert ("Write the file ",write," ",ErrorDescription(GetLastError()));
  FileClose(handle1);

  handle2 = FileOpen(File_Name,FILE_BIN|FILE_READ);// File opening
     Alert ("Opening handle 2 ",handle2," ",ErrorDescription(GetLastError()));
  write = FileReadArray(handle2,Erray2, 0, 4);
     Alert ("Read the file ",write," ",ErrorDescription(GetLastError()));
  FileClose(handle2); 
  
  string txt = DoubleToStr(Erray2[0,0], 8)+" | "+DoubleToStr(Erray2 [1,0],8)+" | "+DoubleToStr(Erray2 [0,1],8)+" | "+DoubleToStr(Erray2 [1,1],8);
     Alert ("See comment too ",txt);
  Comment ("\n",txt);

  return (0);
  }
 
phi.nuts:

You posting an incomplete code. What is the value of write ?

The reason why when fractalup = 0 the data is also 0 is because you using the the FILE_WRITE without FILE_READ. That way, the file operation overwrite/delete the previous data. Try to use


I wrote this working script below base on your first code using binary file, but I thought WHRoeder's answer is sufficient enough so I didn't post it



int handle1,handle2,write;                               // File descriptor
string File_Name="mydata1.csv";                          // File name
string Erray[2,1];                                       // Array 
string read;
Erray[0,0]=fractalup;
Erray[1,0]=fractaldown;
   {
   handle1=FileOpen(File_Name,FILE_CSV|FILE_WRITE,";");  //File opening
   
   if(fractalup>0)
      {
      FileWrite(handle1,Erray[0,0]);      //Writing to the file
         if(write < 0)                          // If failed
            {
            FileClose(handle1);                // File closing
            return;                             // Exit start()      
            }
      }
      
   //else FileClose(handle1);                          // File closing
   Alert("The ",File_Name," file updated.");    // Message
   FileClose(handle1);
   }
//------------------------------------------------------------------------
   {
   handle2=FileOpen(File_Name,FILE_CSV|FILE_READ,";");  //File opening
   read=FileReadString(handle2);
   pastfractalup=StrToDouble(read);
   FileClose(handle2);
   }

 sorry i didnt post the file entirely, now the file is nearly posted, the fractalup may =0 or 1.2345

the result is the same after using the file read,anymore suggestion?
if  fractalup>0, it wrote the value of fractalup,this is done perfectly
but then when fractalup=0, the data in the file also became 0, should it be kept?
why it still gone blank?

 

I'm not so sure what you're looking for, so I re-write the code and I hope it fix this time

#include <stdlib.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {   
  PlaySound ("wait.wav");
  
  int handle1, handle2, write;       // File variable
  string File_Name="mydata1.csv";    // File name
  string Erray[2,1];                 // Array ==>> this array Erray [2, 1] is actually the same like Erray [2]
  string read;

  double fractalup = 1.234567890, fractaldown = 2.345678901, pastfractalup;

  Erray[0,0] = fractalup;
  Erray[1,0] = fractaldown;

   {
   if(fractalup > 0)
      {
      if (handle1 == 0)
        {
         handle1 = FileOpen(File_Name,FILE_CSV|FILE_WRITE,";");  //--- File opening
         if (handle1 > 0)
            {
            write = FileWrite(handle1,Erray[0,0]);               //--- Writing to the file
            if (write <= 0)
                Print ("Failed writing to file handle1 ", ErrorDescription (GetLastError()));
                else
                Print ("Success writing to file handle1 with ",write," character written.");
            FileClose (handle1);
            handle1 = 0;
            //return;                                              //--- exit start
            }
            else
            {
            Print ("Failed to open file handle 1 ", ErrorDescription (GetLastError())); 
            }
        }
     }
   }
//------------------------------------------------------------------------
   {
   handle2 = FileOpen(File_Name,FILE_CSV|FILE_READ,";");  //File opening
   read = FileReadString(handle2);
   pastfractalup = StrToDouble(read);
   if (handle2 > 0)
      {
      FileClose(handle2);
      handle2 = 0;
      }
   Print ("Value from handle2 is ",read);
   }
 
thanks you very much, i see what is my problem already, i should put the condition first before open a file and close it right after, thx a lot  phi.nuts
 
int handle1,handle2,handle3,write;                               // File descriptor
string File_Name="fractalup.csv";                          // File name
string File_Name2="fractaldown.csv"; 
string Erray[2,1];                                       // Array 
string read;
Erray[0,0]=fractalup;
Erray[1,0]=fractaldown;
   
      {
      if(fractalup>0)                                             //store fractal to serve as entry Point
         {
         handle1=FileOpen(File_Name,FILE_CSV|FILE_WRITE,";");     //File opening
         FileWrite(handle1,Erray[0,0]);                           //Writing to the file
            if(write < 0)                                         // If failed
               {
               FileClose(handle1);                                // File closing
               return;                                            // Exit start()      
               }
         FileClose(handle1);
         //Alert("The ",File_Name," file updated.");                // Message
         }
      if (fractaldown>0)
         {
         handle2=FileOpen(File_Name2,FILE_CSV|FILE_WRITE,";");     //File opening
         FileWrite(handle2,Erray[1,0]);                           //Writing to the file
            if(write < 0)                                         // If failed
               {
               FileClose(handle2);                                // File closing
               return;                                            // Exit start()      
               }
         FileClose(handle2);
         //Alert("The ",File_Name," file updated.");                // Message
         }
      }
//------------------------------------------------------------------------
      {
      handle3=FileOpen(File_Name,FILE_CSV|FILE_READ,";");         //File opening
      read=FileReadString(handle3);
      pastfractalup=StrToDouble(read);
      FileClose(handle3);
      }
      {
      handle3=FileOpen(File_Name2,FILE_CSV|FILE_READ,";");         //File opening
      read=FileReadString(handle3);
      pastfractaldown=StrToDouble(read);
      FileClose(handle3);
      }
hi can anyone show me how to simplify the code above?