Fputs From Parameter

Hi

If I want to keep adding text to the file.txt by inputing the text from parameter, how should I do it? And how to fgets the text at the first line of file.txt only.

Plot( C, "Price", colorDefault, styleCandle );

_SECTION_BEGIN("Add Entry"); 
// create folder for exporting purposes
fmkdir( "C:\\MyFile\\" );

function PutValue( String )
{
    fh = fopen( "c:\\MyFile\\file.txt", "w" );

    if( fh )
    {
        fputs( String, fh );
        fclose( fh );
    }
    return fh;
}

EntryDate = ParamDate("Date", "01/02/2018", 0);
toSave = ParamTrigger("Save data ", "Save it");

if (toSave)
{
   PutValue( EntryDate );
}

_SECTION_END();

Please help.

Thanks.

It depends which kind of date string you want to export.
https://www.amibroker.com/guide/afl/paramdate.html

format = 1 will return date to string according to windows system settings

EntryDate = ParamDate("Date", "01/02/2018", 1);

format = 2 will return datetime number. It would require additional function DateTimeToStr.

EntryDate = DateTimeToStr( ParamDate("Date", "01/02/2018", 2) );

Another option to DateTimeToStr is DateTimeFormat
https://www.amibroker.com/guide/afl/datetimeformat.html

EntryDate = DateTimeFormat( "%Y-%m-%d", ParamDate("Date", "01/02/2018", 2) );

Hi

I am succesfully put the entry date to the file.txt, but now if I change the date and add it, the problem is it erases the previous added date, I want it to keep adding the date whenever I press save.

Thanks.

Change fopen function to use append mode.

function PutValue( String )
{
    fh = fopen( "c:\\MyFile\\file.txt", "a" );

    if( fh )
    {
        fputs( String + "\n", fh );
        fclose( fh );
    }
    return fh;
}

Hi

Thank you, codejunkie. Is it the same if I want to read the first line only? For example I want to fgets my latest trade date only.

Try this one. It has not been tested.

function PutValue( String )
{
    fh = fopen( "c:\\MyFile\\file.txt", "a" );

    if( fh )
    {
        fputs( String + ";", fh );
        fclose( fh );
    }
    return fh;
}

EntryDate = ParamDate("Date", "01/02/2018", 0);
toSave = ParamTrigger("Save data ", "Save it");

if (toSave)
{
   PutValue( EntryDate );
}

line = "";
fh = fopen("c:\\MyFile\\file.txt", "r" );
// taken from AmiBroker code snippets
if( fh ) 
{ 
   while( ! feof( fh ) ) 
   { 
      line = fgets( fh ); // read a line of text
      _TRACE( line );    
   }

    fclose( fh ); 
} 
else 
{ 
   Error("ERROR: file can not be open"); 
} 

lastdate = StrExtract( line, Strcount(StrTrim(line, ","), ","), ',');

Hi

Thank you, codejunkie. It's working now.