I placed "AAPL.csv" into the MetaTrader terminal folder's Files subfolder (MQL4/Files) to be accessible by the EA. The structure of this csv is as follows:
I want to read this, as well as many other similar csv files. All files have different lengths. My question is, what is the best practice when reading variable-length files? For now, I managed to read the content of my file by creating a 2-dimensional array:
although this is poor programming (what if the file only has 500 rows?) and it can still fail if I encounter a csv that is longer (what if file has 1000001 rows?). I tried using a dynamic array:
but it returns '[' - invalid index value error. Yet another idea I had to use the FileSize() function and allocate just-the-necessary amount of memory to the 2-dimensional array. However,
yielded a size that equals the product of column numbers and row numbers. I was hoping to obtain a row_count and col_count and use them to define s:
My full working code:
Inserted Code
Date,Open,High,Low,Close,Adj Close,Volume 1980-12-12,0.1283479928970337,0.1289059966802597,0.1283479928970337,0.1283479928970337,0.10092189908027649,469033600 1980-12-15,0.12221000343561172,0.12221000343561172,0.12165199965238571,0.12165199965238571,0.09565676748752594,175884800
I want to read this, as well as many other similar csv files. All files have different lengths. My question is, what is the best practice when reading variable-length files? For now, I managed to read the content of my file by creating a 2-dimensional array:
Inserted Code
string s[7][1000000];
Inserted Code
string s[7][];
Inserted Code
int handle=FileOpen(FileName,FILE_CSV|FILE_READ,","); if(handle>0) { int size = FileSize(handle); ...
Inserted Code
string s[col_count][row_count];
Inserted Code
extern string FileName = "AAPL.csv"; int init() { int row=0,col=0; string s[7][1000000]; ResetLastError(); int handle=FileOpen(FileName,FILE_CSV|FILE_READ,","); if(handle>0) { //int size = FileSize(handle); //Print(size); while(True) { string temp = FileReadString(handle); if(FileIsEnding(handle)) break; //FileIsEnding = End of File s[col][row]=temp; if(FileIsLineEnding(handle)) //FileIsLineEnding = End of Line { col = 0; //reset col = 0 for the next row row++; //next row } else { col++; //next col of the same row } } FileClose(handle); } else { Comment("File "+FileName+" not found, the last error is ", GetLastError()); } return(0); } int start() { return(0); } //+------------------------------------------------------------------+