- HELP COPYING FILE TO ANOTHER FILE
- Questions from a "dummy"
- How to read a single cell from a CSV file?
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. |
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
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); }
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]
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?
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.
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.
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
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use