Jump to end of text file

hi,

i am writing some code to save data in a database. I want to update a file if it already exists. Basically just push 1 button and it updates all the data files or creates new ones if they do not yet exist. So I have something that works I just wonder if one can jump to the end of a text file. So now I have written this (see code below).

If the file is found it will extract the datetime of the last line in the text file which then later will be used to append data to this file. However, it has to go through the entire file before it gets to the last line. First it didn't work because in the Tools | Preferences | AFL I had the endless loop detection threshold set too low.

So, is there some trick to jump to the end of file and read the last line?

function findLastLineInFile()
{
    Say( "Find last line in file" );
    str = strold = "";
    fh = fopen( "D:\\AmibrokerData\\" + Name() + ".txt", "r" );

    if( fh )
    {
        while( !feof( fh ) )
        {
            strold = str;
            str = fgets( fh );
        }

        str = StrExtract( strold, 1 );
        val = StrToDateTime( str );
    }
    else
    {
        printf( "ERROR: file can not be found (does not exist)" );
        val = 0;
        str = "";
    }

    fclose( fh );
}

You don't need to go thru the file, you just need to use append mode:

http://www.amibroker.com/kb/2016/01/27/how-to-write-to-single-shared-file-in-multi-threaded-scenario/

oh really? thanks will test it

i already used append but didn't know it will automatically find the correct data to append. My full code was this (see below). So if it found the file it would use append. But it can be done easier, will check it out, thanks

_SECTION_BEGIN( "Export 1Min Data" );

ExportOHCL = ParamTrigger( "Export Data", "EXPORT" );
TF = interval();
str = strold = "";
val = 0;

function findLastLineInFile()
{
    Say( "Find last line in file" );
    str = strold = "";
    fh = fopen( "D:\\AmibrokerData\\" + Name() + ".txt", "r" );

    if( fh )
    {
        while( !feof( fh ) )
        {
            strold = str;
            str = fgets( fh );
        }

        str = StrExtract( strold, 1 );
        val = StrToDateTime( str );
    }
    else
    {
        printf( "ERROR: file can not be found (does not exist)" );
        val = 0;
        str = "";
    }

    fclose( fh );
}

if( ExportOHCL AND TF == in1Minute )
{

    findLastLineInFile();

    // new file, file does not yet exist
    if( val == 0 AND str = "" )
    {
		_TRACE( "export 1 " );
        Say( "start export One" );
        
		fmkdir( "C:\\AmibrokerData\\" );

        // open file for writing
        // file name depends on currently processed ticker
        fh = fopen( "D:\\AmibrokerData\\" + Name() + ".txt", "w" );

        // proceed if file handle is correct
        if( fh )
        {
            dt = DateTime();

            // write header line
            fputs( "Ticker,Date/Time,Open,High,Low,Close,Volume,BuyVolume,SellVolume\n", fh );

            // iterate through all the bars

            for( i = 0; i < BarCount; i++ )
            {
                // write ticker name
                fputs( Name() + "," , fh );

                // write date/time information
                fputs( DateTimeToStr( dt[i] ) + ",", fh );

                //write quotations and go to the next line
                qs = StrFormat( "%g,%g,%g,%g,%g.%g,%g\n", O[i], H[i], L[i], C[i], V[i], BuyVolume[i], SellVolume[i] );
                fputs( qs, fh );

            }

            // close file handle
            fclose( fh );
            Say( "finished export one" );
        }
    }
    else

        // the export file already exists just append from last saved data
        if( val != 0 AND str != "" )
        {
			_TRACE( "export 2 " );
            Say( "start export Two" );

            // open file for appending
            // file name depends on currently processed ticker
            fh = fopen( "D:\\AmibrokerData\\" + Name() + ".txt", "a" );

            // proceed if file handle is correct
            if( fh )
            {
                dt = DateTime();
               
				//fputs( "sep\n", fh ); // test separator
                // iterate through all the bars
                for( i = 0; i < BarCount; i++ )
                {
                    if( dt[i] > val )
                    {
                        // write ticker name
                        fputs( Name() + "," , fh );

                        // write date/time information
                        fputs( DateTimeToStr( dt[i] ) + ",", fh );

                        //write quotations and go to the next line
                        qs = StrFormat( "%g,%g,%g,%g,%g.%g,%g\n", O[i], H[i], L[i], C[i], V[i], BuyVolume[i], SellVolume[i] );
                        fputs( qs, fh );
                    }
                }

                // close file handle
                fclose( fh );
                Say( "finished export two" );
            }
        }
}

_SECTION_END();
1 Like

You are overthinking things, append mode "a" creates a file if it doesn't exist, or appends at the end of the file automatically if it already exists

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.