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
GumRai:
Actually the FileOpen in write mode scratches the data file before the seek.
..
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:
Actually the FileOpen in write mode scratches the data file before the seek.
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 :)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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.