From a file to an array.

 

Hello. I am trying to copy data from a file to an array but even with the documentation and similar topics I can't seem to point out where the problem is.

double destination[];

ResetLastError();
int handle=FileOpen("Ratio Data.csv",FILE_READ|FILE_CSV);
   if (handle!=INVALID_HANDLE)
      {
         FileWriteArray(handle,destination);
         int size=ArraySize(destination);
         
         Print(size);
         
         FileClose(handle);
      }
   else
      Print ("File open failed ",GetLastError());

I keep getting the zero as the size of the array no matter how much i tweak it. What could be the problem?

Thanks!

Documentation on MQL5: File Functions / FileCopy
Documentation on MQL5: File Functions / FileCopy
  • www.mql5.com
//| Script program start function                                    | //
 

Try this (I once wrote it for MT4):

int readCsvFile(const string fName, string &allLines[]) {
   int nI, nL=-1, hdl  = FileOpen(fName,FILE_READ|FILE_SHARE_READ|FILE_BIN|FILE_COMMON); // for a file in COMMON-folder!!
   if (hdl == INVALID_HANDLE) { Alert(fName," FileOpen in COMMON FAILED: ",_LastError); FileClose(hdl); return(-1); }
   string f = FileReadString(hdl,(int)FileSize(hdl));
   FileClose(hdl);
   nL = StringSplit(f,StringGetCharacter("\n",0),allLines);
   return(nL);
}

(No guaranty)

 
michealm:

Hello. I am trying to copy data from a file to an array but even with the documentation and similar topics I can't seem to point out where the problem is.

I keep getting the zero as the size of the array no matter how much i tweak it. What could be the problem?

Thanks!

And why are you using FileWriteArray() instead of FileReadArray()?

Your code totally has no attempt of copying anything into the Array.

 
Carl Schreiber:

Try this (I once wrote it for MT4):

(No guaranty)

Unfortunately it doesn't work. Thanks though!

 
Nelson Wanyama:

And why are you using FileWriteArray() instead of FileReadArray()?

Your code totally has no attempt of copying anything into the Array.

I have tried both of them over and over with no results.

 
  1. Mureithi Mbugua: What could be the problem?
    int handle=FileOpen("Ratio Data.csv",FILE_READ|FILE_CSV);
       
             FileWriteArray(handle,destination);

    The problem is you need to read the documentation. FileReadArray is clearly stated to only work with binary files, and you are opening a text file.

  2. Murray Mbugua: I have tried both of them over and over with no results.
    Insanity: doing the same thing over and over again and expecting different results.
              Unknown
 
William Roeder:
  1. ... and you are opening a text file.

lol.. if you only knew how much i have struggled with that. i am pretty new to programming so forgive me if i make such boring errors. thanks for helping me out. again.


William Roeder:

2.

Insanity is often the logic of an accurate mind overtasked.

-Oliver Wendell Holmes

 
William Roeder:

I don't know if I am supposed to open a new topic for this but I have a follow up question. Why is it that this code...

{
int x=5;
int Arrayhold[9999];

int fileHandle=FileOpen("test one.bin",FILE_READ|FILE_WRITE|FILE_BIN);
for (int i=0; i<50; i++)
{
   int ans=x*i;
   Arrayhold[i]=ans;
   
   FileWrite(fileHandle,ans);
}
FileClose(fileHandle);


}

... creates an empty bin file while this one..

{
int x=5;
int Arrayhold[9999];

int fileHandle=FileOpen("test one.csv",FILE_READ|FILE_WRITE|FILE_CSV);
for (int i=0; i<50; i++)
{
   int ans=x*i;
   Arrayhold[i]=ans;
   
   FileWrite(fileHandle,ans);
}
FileClose(fileHandle);

... creates a csv file with the required values?

 
I am trying to Print datetime variables (that have been copied from a file to an array) but all I get is "wrong datetime". I think with a solution to this i will be able to design a workaround that.
 
Murray Mbugua:

lol.. if you only knew how much i have struggled with that. i am pretty new to programming so forgive me if i make such boring errors. thanks for helping me out. again.


Insanity is often the logic of an accurate mind overtasked.

-Oliver Wendell Holmes

Haha. Nice one😂

 
Murray Mbugua:
I am trying to Print datetime variables (that have been copied from a file to an array) but all I get is "wrong datetime". I think with a solution to this i will be able to design a workaround that.

You could try using a csv for it, if you intend to read the file your self. You cannot read a .bin file like a normal txt file, unless you read it from within your program then print it's values. (from my experience)

You can format your datetime variables to int, then convert them back on reading since .bin files DO NOT take strings (unless you use StringToCharArray() then convert it back on reading). There's always a work-around depending on what you're trying to do.

But a read string can be converted to datetime(documented) but you'll have to understand the formatting syntax. 

Press f1 on your editor to get most info.

Reason: