Download MetaTrader 5
To add comments, please log in or register
Having difficulties in understanding the Trading Signals service? Read the article!
macpee
287
macpee 2016.03.20 10:52 

Hello comrades, please consider the following MQL4 codes. The first function which is named "OpenWrite" is meant to write only one of USD or EUR, etc, to a text file. The second function named "OpenRead" is meant to read the content of the file. The first function actually writes to the file. However, the second function does not read anything, instead it erases the content of the file. Please what could be the reason why the second function erases the content of the file rather than read it. Please see the codes below. Thank you in advance.

void OpenWrite()
{
int CurrencyHandle=FileOpen("currency.txt",FILE_WRITE|FILE_CSV|FILE_TXT);
if(CurrencyHandle!=INVALID_HANDLE)
     FileWrite(CurrencyHandle,Symbol());    //meant to write only one of USD, EUR, etc, to a text file.
else            
    {
     Print("Operation FileOpen failed, error ",GetLastError());
     FileClose(CurrencyHandle);
    }
}

void OpenRead(){
string Curren;
int CurrencyHandle=FileOpen("currency.txt",FILE_WRITE|FILE_CSV|FILE_TXT);
if(FileSeek(CurrencyHandle,1,SEEK_SET)==true)
   {
    Curren=FileReadString(CurrencyHandle,3);   //meant to read USD, EUR, etc, from text file
    FileClose(CurrencyHandle);
    Print("Currency= ",Curren);
   }
}
Keith Watford
Moderator
7906
Keith Watford 2016.03.20 11:19  

I must admit that I don't work with files much and usually have to look up everything when I do.

void OpenWrite()
{
int CurrencyHandle=FileOpen("currency.txt",FILE_WRITE|FILE_CSV|FILE_TXT);
if(CurrencyHandle!=INVALID_HANDLE)
     FileWrite(CurrencyHandle,Symbol());    //meant to write only one of USD, EUR, etc, to a text file.
else            
    {
     Print("Operation FileOpen failed, error ",GetLastError());
     FileClose(CurrencyHandle);
    }
}

Here you only attempt to close the file if you don't actually open the file. That seems redundant. If you are successful in opening the file, you don't close it.


void OpenRead(){
string Curren;
int CurrencyHandle=FileOpen("currency.txt",FILE_WRITE|FILE_CSV|FILE_TXT);
if(FileSeek(CurrencyHandle,1,SEEK_SET)==true)
   {
    Curren=FileReadString(CurrencyHandle,3);   //meant to read USD, EUR, etc, from text file
    FileClose(CurrencyHandle);
    Print("Currency= ",Curren);
   }
}

You open the file for writing, not for reading.

You seek the beginning of the file and I guess that this overwrites any existing data with nothing as you don't write anything to the file

Ovo Cz
2963
Ovo Cz 2016.03.20 11:30  
GumRai:

..

You seek the beginning of the file and I guess that this overwrites any existing data with nothing as you don't write anything to the file

Actually the FileOpen in write mode scratches the data file before the seek.
Keith Watford
Moderator
7906
Keith Watford 2016.03.20 11:38  
Ovo:
Actually the FileOpen in write mode scratches the data file before the seek.

Thanks Ovo, I didn't realise that.

Learn something new every day :)

macpee
287
macpee 2016.03.20 15:48  
GumRai:

Thanks Ovo, I didn't realise that.

Learn something new every day :)

Thank you, Ovo and GumRai. That means I was right in Opening the file for reading and also I can close it after reading, contrary to GumRai. I have finally gotten the required results. The read function now has an output.
/
To add comments, please log in or register