How to Read/Write to a specific line in external file?

Hello guys,
I have a very simple text file with 4 lines of text.

I want to change specific lines with appropriate value when signal arrives.
for example:

LongOpen1
LongExit0
ShortOpen0
ShortExit0

Or like this:

LongOpen0
LongExit1
ShortOpen1
ShortExit0

So in Amibroker I now have file where I manually insert text to each string and assign to variables:

function ResetTradeFile()
{
	fh = fopen( FileWithCommands, "w" );

	LO = fputs("LongOpen0\n", fh);
	LE = fputs("LongExit0\n", fh);
	SO = fputs("ShortOpen0\n", fh);
	SE = fputs("ShortExit0\n", fh);
	
	fclose( fh ); 
}

How would I go about replacing particular string now?
Is there any built-in function in AFL that can help me?

Or should I just store commands inside a matrix and rewrite the whole file with updated matrix?

Thank you

@Konstantin

for writing, something like this:

FileWithCommands = "C:\\temp\\FileWithCommands.txt";

function ResetTradeFile(BuySig, SellSig, ShortSig, CoverSig)
{
	fh = fopen( FileWithCommands, "w" );
	if (fh) 
	{
		fputs(StrFormat("LongOpen%1.0f\n", BuySig), fh);
		fputs(StrFormat("LongExit%1.0f\n", SellSig), fh);
		fputs(StrFormat("ShortOpen%1.0f\n", ShortSig), fh);
		fputs(StrFormat("ShortExit%1.0f\n", CoverSig), fh);
		fclose( fh ); 
	}
	else 
	{ 
	   Error("ERROR: File " + fileWithCommands + " cannot be open for writing."); 
	}
}

// ResetTradeFile(1, 0, 0, 0);

// ResetTradeFile(0, 1, 0, 0);

// ResetTradeFile(0, 0, 1, 0);

ResetTradeFile(0, 0, 0, 1);

Please, note that fputs() does not return any value (nothing).

3 Likes

Thank you @beppe for such swift and detailed answer!

Please, note that fputs() does not return any value (nothing).

Yes I've realized that now. So part of my code doesn't make sense ^^'
Thank you once again for clarifying this approach to me.