FileWrite - FileRead Question

 

I'm trying to figure out how to store a value from my EA into a file, then retrieve it and reinsert it back into the EA. So that, in the event of a loss of connection, my EA would be able to resume calculations for trading without opening, unwanted, duplicate trades, due to a loss of it's values.

Spread = ((Ask - Bid)*100000);

// ----------------------------------------------------------------------------- FileWrite ----------------

int SpreadI;
SpreadI=FileOpen("SpreadI", FILE_CSV|FILE_WRITE, ';');

if(SpreadI>0)
{
FileWrite(SpreadI, Spread);
FileClose(SpreadI);
}

// ----------------------------------------------------------------------------- FileRead ----------------


int SpreadO;
int SpreadV;
SpreadO=FileOpen("SpreadI",FILE_CSV|FILE_READ,';');
if(SpreadO>0)
{
SpreadV = FileReadNumber(SpreadI);
FileClose(SpreadO);
Spread = SpreadO;
Alert("SpreadO = ",SpreadO," Spread = ",Spread);

Storing the value, of Spread, to a file is not the problem. Retrieving it is. I get a value of "1" instead of the value of Spread stored. "16"

Am I doing something wrong? Missing something? Or missing the point?

Thanks!

 

Your FileWrite() has converted the number to a string.

So to read your value use:

SpreadV = StrToInteger(FileReadString(SpreadI));


CB

 
cloudbreaker wrote >>

Your FileWrite() has converted the number to a string.

So to read your value use:

SpreadV = StrToInteger(FileReadString(SpreadI));

CB

Ooops! i Figured out my mistake.

Had:

Spread = SpreadO;
Alert("SpreadO = ",SpreadO," Spread = ",Spread);

Should have been:

Spread = SpreadV;
Alert("SpreadV = ",SpreadV," Spread = ",Spread);

Sometimes they stare you right in the face!

Thanks CB!

Reason: