Could someone tell me why This EA is not working.

 

I am trying to Subtract from the Text the word Sell, but, the program below can not read the file. My test.txt is stored inC:\Program Files\MetaTrader\experts\files and it contains this Text: SELL EURUSDPrice=1.32336, Could someone please help.

int start()
{
string File_Name="test.txt",Text;
int Handle_Open;
{ Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_READ, ';');
if (Handle_Open<0)
{ Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_WRITE, ';');
Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_READ, ';');
}
if(Handle_Open>0)
{ Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_WRITE, ';');
FileClose(Handle_Open);
}
}
int g=FileOpen(File_Name,FILE_WRITE|FILE_READ,";");
if(g<0) // File opening fails
{
if(GetLastError()==4103) // If the file does not exist,..
Alert("No file named ",File_Name);//.. inform trader
else // If any other error occurs..
Alert("Error while opening file ",File_Name);//..this message
PlaySound("Bzrrr.wav"); // Sound accompaniment
return; // Exit start()
}
while(FileIsEnding(g)==false) // While the file pointer..
{
FileSeek(g,0,SEEK_SET);
string ordertype=StringSubstr(FileReadString(g),0,4);
int Error=GetLastError();
if(Error>0)Alert("Error reading String. Error#",Error);
Alert("Order Type=",ordertype);
if(FileIsEnding(g)==true) // File pointer is at the end
break;
}
FileClose(g);


//----
//----
return(0);
}
//+----------------------

 
Everything's wrong. Read carefully about file functions.
 
Roger:
Everything's wrong. Read carefully about file functions.


Dear Roger:

Thank you very much for your respond. I am new to file function. Could you please tell me what mistakes I have in my EA or direct me to the right direction.

TIA

 

read this, https://book.mql4.com/functions/files


you open files very often, more then neccesary,

FILE_CSV|FILE_READ - opens existing files only. in read only

FILE_CSV|FILE_WRITE - opens, and if it does'nt exist it creates it, in write only.

FILE_CSV|FILE_WRITE|FILE_READ - opens, and if it does'nt exist it creates it, in write and read.


every time you open a file you must close it, either in function or at deinit(). if you close file in deinit, you might consider fileflush() (if you write to it).


Ohh.. and next time use the "SRC" button

 
forexaccount:


Dear Roger:

Thank you very much for your respond. I am new to file function. Could you please tell me what mistakes I have in my EA or direct me to the right direction.

TIA


int start()
{
string File_Name="test.txt",Text;
int Handle_Open;
{ Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_READ, ';');//wrong file name
if (Handle_Open<0)
{ Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_WRITE, ';');//didn't close previos handle
Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_READ, ';');// the same
}
if(Handle_Open>0)
{ Handle_Open=FileOpen("File_Name", FILE_CSV|FILE_WRITE, ';');//the same
FileClose(Handle_Open);
}
}
int g=FileOpen(File_Name,FILE_WRITE|FILE_READ,";");// no csv
if(g<0) // File opening fails
{
if(GetLastError()==4103) // If the file does not exist,..
Alert("No file named ",File_Name);//.. inform trader
else // If any other error occurs..
Alert("Error while opening file ",File_Name);//..this message
PlaySound("Bzrrr.wav"); // Sound accompaniment
return; // Exit start()
}
while(FileIsEnding(g)==false) // While the file pointer..
{
FileSeek(g,0,SEEK_SET);//what is this?
string ordertype=StringSubstr(FileReadString(g),0,4);
int Error=GetLastError();
if(Error>0)Alert("Error reading String. Error#",Error);
Alert("Order Type=",ordertype);
if(FileIsEnding(g)==true) // File pointer is at the end//what is this?
break;
}
FileClose(g);
//----
//----
return(0);
}
 
Dear Roger:

Thank you very much for your help. I will follow your instructions and will see what happens.

 
enotrek:

read this, https://book.mql4.com/functions/files


you open files very often, more then neccesary,

FILE_CSV|FILE_READ - opens existing files only. in read only

FILE_CSV|FILE_WRITE - opens, and if it does'nt exist it creates it, in write only.

FILE_CSV|FILE_WRITE|FILE_READ - opens, and if it does'nt exist it creates it, in write and read.


every time you open a file you must close it, either in function or at deinit(). if you close file in deinit, you might consider fileflush() (if you write to it).


Ohh.. and next time use the "SRC" button


Thank you very much for your points. Could you please tell me what is SRC and where can I find it?
 
forexaccount:

Thank you very much for your points. Could you please tell me what is SRC and where can I find it?
 
Viffer:


Thank you.
Reason: