Download MetaTrader 5

Reading only one cell of a group of records with several columns

To add comments, please log in or register
macpee
498
macpee  
Hello comrades. How exactly can I retrieve only a cell in a record using the FileReadString() function? I have the following code. In the code I want to read only the second column of only the third row. How can I go about doing that. Thank you in advance
void start()
int AskDis;
{
string Currency="USD"; int AskDistance=100,OrderSpacing=50,Nest=5,StopProfit=500,StopStop=200;
int CurrencyHandle=FileOpen("MyFile.txt",FILE_WRITE|FILE_CSV|FILE_TXT); 
for (int i=1;i<=10;i++)
   {
    FileWrite(CurrencyHandle,i,Currency,AskDistance,OrderSpacing,Nest,StopProfit,StopStop);
   }
FileClose(CurrencyHandle);

int CurrencyHandle=FileOpen("MyFile.txt",FILE_READ|FILE_CSV|FILE_TXT);
int Space=FileReadString(CurrencyHandle,AskDistance);
Print("Space Handle= ",AskDis);
}
I want to read the value of AskDistance using the variable AskDis in the FileReadString() command, and not other records. I have seen where they make use of the variable pos or pos[i], etc. But I did not understand the ideas. My idea is to retrieve a unit of data from a set of records.
whroeder1
16279
whroeder1  
macpee: I want to read only the second column of only the third row. How can I go about doing that.
You read all the columns of the row and ignore the rest.
Keith Watford
Moderator
10570
Keith Watford  
CurrencyHandle=FileOpen("MyFile.txt",FILE_READ|FILE_TXT);
string Space=FileReadString(CurrencyHandle);
string ss[];
StringSplit(Space,';',ss);
// AskDistance should be array element 2
Print("AskDistance = ",ss[2]);

Doing it this way would allow you to access any column value. ie the value for currency will be stored in ss[1]

Note the removal of FILE_CSV as you need to read the complete line, not just to the delimiter

macpee
498
macpee  

Thank you GumRai. But consider the following codes. I want to read the cell at the third row and second column. That is, "EUR". I tried to work it out using common sense by Printing as follows: Print("Currency= ",cell[3,2]); But it returned error (wrong dimension). See code below. Thank you in advance.

void start(){
int CurrencyHandle=FileOpen("MyFile.txt",FILE_WRITE|FILE_TXT|FILE_CSV); 
FileWrite(CurrencyHandle,  1,  "USD",  100,  1.3647);
FileWrite(CurrencyHandle,  2,  "GBP",  200,  0.3023);
FileWrite(CurrencyHandle,  3,  "EUR",  300,  1.5678);
FileWrite(CurrencyHandle,  4,  "JPY",  400,  4.4678);
FileClose(CurrencyHandle);

CurrencyHandle=FileOpen("MyFile.txt",FILE_READ|FILE_TXT);
string Space=FileReadString(CurrencyHandle);
string cell[];
StringSplit(Space,';',cell);
Print("Currency= ",cell[3,2]);
FileClose(CurrencyHandle);
}
Keith Watford
Moderator
10570
Keith Watford  

You have declared the array cell as single dimensional, so you cannot use it as a 2 dimension array.

      CurrencyHandle=FileOpen("MyFile.txt",FILE_READ|FILE_TXT);
      for(int x=1;x<5;x++)
        {
         string Space=FileReadString(CurrencyHandle);
         string cell[];
         StringSplit(Space,';',cell);
         Print("Line ",x," Currency= ",cell[1]);
        }
      FileClose(CurrencyHandle);

Arrays start at element 0, so the second element is accessed with [1]

macpee
498
macpee  
GumRai:

You have declared the array cell as single dimensional, so you cannot use it as a 2 dimension array.

Arrays start at element 0, so the second element is accessed with [1]

The variable x that you specified has no role in determining what column is to be printed. It is only a running variable that loops five times. The program is still printing only the first row. I think there should be a function of x that determines the row to be put into consideration in reading the file. But no such function has been specified. Please how can I go about it? Besides, you have not told me how to specify the 2-dimensional array.
Keith Watford
Moderator
10570
Keith Watford  
macpee:
The variable x that you specified has no role in determining what column is to be printed. It is only a running variable that loops five times. The program is still printing only the first row. I think there should be a function of x that determines the row to be put into consideration in reading the file. But no such function has been specified. Please how can I go about it?

Your code

Print("Currency= ",cell[3,2]);

didn't work so I gave you an example of what does work.

As you have "currency= " in your code, I showed you how to print all values in the currency column.

Now that you have an example, you can continue with whatever you want to do.

macpee
498
macpee  
That is the point. Your example did not work either. Look at it very well. The x loop did not determine that row2, row3, etc be printed. It only prints row1 five times. I need a working example please. Thank you in advance.
Zirkon
121
Zirkon  
macpee:
That is the point. Your example did not work either. Look at it very well. The x loop did not determine that row2, row3, etc be printed. It only prints row1 five times. I need a working example please. Thank you in advance.

You cannot just read a value in row X, column Y. You have to read all the file and then keep the values you want.

You already got several examples from Gumrai.

Keith Watford
Moderator
10570
Keith Watford  
macpee:
That is the point. Your example did not work either. Look at it very well. The x loop did not determine that row2, row3, etc be printed. It only prints row1 five times. I need a working example please. Thank you in advance.

Check your code against what I showed you. You have done something wrong

I put this in an EA

bool FirstTick=true;
void OnTick()
  {
   if(FirstTick)
     {
      int CurrencyHandle=FileOpen("MyFile.txt",FILE_WRITE|FILE_TXT|FILE_CSV);
      FileWrite(CurrencyHandle,  1,  "USD",  100,  1.3647);
      FileWrite(CurrencyHandle,  2,  "GBP",  200,  0.3023);
      FileWrite(CurrencyHandle,  3,  "EUR",  300,  1.5678);
      FileWrite(CurrencyHandle,  4,  "JPY",  400,  4.4678);
      FileClose(CurrencyHandle);

      CurrencyHandle=FileOpen("MyFile.txt",FILE_READ|FILE_TXT);
      for(int x=1;x<5;x++)
        {
         string Space=FileReadString(CurrencyHandle);
         string cell[];
         StringSplit(Space,';',cell);
         Print("Line ",x," Currency= ",cell[1]);
        }
      FileClose(CurrencyHandle);
     }
   FirstTick=false;
//---

  }

and this was printed in the log, it does not print row 1 five times, in fact there are only 4 prints

0       15:21:43.484    Expert ###1 EURUSD,H1: loaded successfully
0       15:21:44.843    ###1 EURUSD,H1: initialized
0       15:21:45.609    ###1 EURUSD,H1: Line 1 Currency= USD
0       15:21:45.609    ###1 EURUSD,H1: Line 2 Currency= GBP
0       15:21:45.609    ###1 EURUSD,H1: Line 3 Currency= EUR
0       15:21:45.609    ###1 EURUSD,H1: Line 4 Currency= JPY
macpee
498
macpee  
Wow! That's great. Once again thanks a lot GumRai. Thanks to all who have contributed.
12
To add comments, please log in or register