Download MetaTrader 5

How to read .txt file character by character in MQL4?

To add comments, please log in or register
Read the interviews of the Automated Trading Championship participants
Proximus
760
Proximus 2013.12.15 22:06 

How to read a .txt file, if it's even possible, character by character ?

Or if its not possible then by separated characters with 1 space.

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2013.12.15 23:19  
Proximus:

How to read a .txt file, if it's even possible, character by character ?

Or if its not possible then by separated characters with 1 space.

Open it in BN and READ mode and read a Byte at a time FileReadInteger() and that will give you ASCII value of a character, more or less . . .
Proximus
760
Proximus 2013.12.16 04:48  
RaptorUK:
Open it in BN and READ mode and read a Byte at a time FileReadInteger() and that will give you ASCII value of a character, more or less . . .

Thanks but for some reason i always get error 4099.Its frustrating...

Am i doing something wrong?


Outside function declaration

int CHARVEKTOR[];
int CHARACTER=0;

Init() function

int FILE=FileOpen("CHARACTER.txt", FILE_READ|FILE_BIN) ;
int k=0;

if(FILE>0)
{
while(!FileIsEnding(FILE))
{
CHARVEKTOR[k]=FileReadInteger(FILE,1) ;
k++;
}
FileClose(FILE);
}


The file CHARACTER.txt is located in MetaTrader\experts\files and also made a copy of it into MetaTrader\tester\files just in case it doesnt regocnize it at the other location.

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2013.12.16 09:17  
Proximus:

Thanks but for some reason i always get error 4099.Its frustrating...

Am i doing something wrong?


Outside function declaration

Init() function


The file CHARACTER.txt is located in MetaTrader\experts\files and also made a copy of it into MetaTrader\tester\files just in case it doesnt regocnize it at the other location.

Your array has zero elements . . .

int CHARVEKTOR[];

. . . unless you are resizing it somewhere ?

Proximus
760
Proximus 2013.12.16 12:24  
RaptorUK:

Your array has zero elements . . .

. . . unless you are resizing it somewhere ?


Yes you are right, i just fixed it

if(FILE>0)
{
while(!FileIsEnding(FILE))
{
ArrayResize(CHARVEKTOR,k+1);
CHARVEKTOR[k]= StrToInteger(FileReadString( FILE, 1))  ;
k++;
}
FileClose(FILE);
}


I also replaced the read integer with read string because if i read integer values it sometimes gave silly values, non corresponding to what was in the text file, so readstring is better for me.

Ex Ovo Omnia
3146
Ex Ovo Omnia 2013.12.16 14:10  
Proximus:

Yes you are right, i just fixed it


I also replaced the read integer with read string because if i read integer values it sometimes gave silly values, non corresponding to what was in the text file, so readstring is better for me.


It is perhaps not a good idea, there are some characters not defined in Windows, and they would be either shifted or missed. Better to stick with the FileReadInteger.
Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2013.12.16 14:25  
Ovo:

It is perhaps not a good idea, there are some characters not defined in Windows, and they would be either shifted or missed. Better to stick with the FileReadInteger.
I'm not even sure it will work, read a string from a CSV file using FILE_CSV Mode OK, but using FILE_BIN will not give a string, will it ?
Proximus
760
Proximus 2013.12.17 06:21  
RaptorUK:
I'm not even sure it will work, read a string from a CSV file using FILE_CSV Mode OK, but using FILE_BIN will not give a string, will it ?


Ovo:

It is perhaps not a good idea, there are some characters not defined in Windows, and they would be either shifted or missed. Better to stick with the FileReadInteger.

No guys it works as it is like a wonder.Since the .txt file only has numbers in it.It's purpose was only to randomize the magic number :)

If i'd use the FileReadInteger.,then it will read very weird data when it reads a space, so the

FileReadString

is better in my case.

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2013.12.17 10:42  
Proximus:

No guys it works as it is like a wonder.Since the .txt file only has numbers in it.It's purpose was only to randomize the magic number :)

Why would you ever want a random MN ?

Set a base MN for your EA then add in an additional 2 digits for the timeframe and make sure you check the Symbol() . . . doesn't this make things unique ?

To add comments, please log in or register