Page 1 of 1

insert text into file text

PostPosted: Fri Jun 28, 2024 7:55 am
by damianodec
Hi,
I'd like insert text into a file txt.


BEFORE
STATE - ITALY FROM ITALY
CITY LECCE FROM LECCE
ADDRESS VIA ROMA 8


AFTER
STATE - ITALY FROM ITALY
NAME - MARIO ROSSI
CITY LECCE FROM LECCE
ADDRESS VIA ROMA 8

thanks

Re: insert text into file text

PostPosted: Fri Jun 28, 2024 9:03 am
by Antonio Linares
Code: Select all  Expand view
   local aLines := hb_aTokens( hb_memoRead( "test.txt" ), CRLF )
   local cText := ""    
   
   AIns( aLines, 2 )
   aLines[ 2 ] = "NAME - MARIO ROSSI"
   
   AEval( aLines, { | cLine | cText += cLine + CRLF } )
   ? hb_memoWrit( "test.txt", cText )
   ? hb_memoRead( "test.txt" )
 

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 6:13 am
by damianodec
hi Antonio, thank you.

I get this:
Error: Unresolved external '_HB_FUN_HB_MEMOREAD' referenced from C:\FWH1709\SAMPLES\TESTTEXT.OBJ

(FiveWin for xHarbour 17.09 - Sep. 2017)

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 7:33 am
by Enrico Maria Giordano
Use MEMOREAD() instead. The Harbour developers love to add useless things... :-(

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 8:16 am
by Antonio Linares
Dear Enrico,

HB_MEMOREAD() vs MEMOREAD(): HB_MEMOREAD() is identical to MEMOREAD() except it won’t truncate the last byte (on non-UNIX compatible systems) if it’s a EOF char.


Code: Select all  Expand view
static HB_BOOL hb_memowrit( HB_BOOL bHandleEOF )
{
   const char * pszFileName = hb_parc( 1 );
   PHB_ITEM pString   = hb_param( 2, HB_IT_STRING );
   HB_BOOL bRetVal    = HB_FALSE;

   if( pszFileName && pString )
   {
      PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL,
                                       FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE |
                                       FXO_TRUNCATE | FXO_SHARELOCK,
                                       NULL, NULL );

      if( pFile != NULL )
      {
         HB_SIZE nSize = hb_itemGetCLen( pString );
         const char * pData = hb_itemGetCPtr( pString );

         while( nSize > 0 )
         {
            HB_SIZE nWritten = hb_fileWrite( pFile, pData, nSize, 0 );
            if( nWritten == 0 || nWritten == ( HB_SIZE ) FS_ERROR )
               break;
            nSize -= nWritten;
            pData += nWritten;
         }
         bRetVal = nSize == 0;

         /* NOTE: CA-Cl*pper will add the EOF even if the write failed. [vszakats] */
         /* NOTE: CA-Cl*pper will not return .F. when the EOF could not be written. [vszakats] */
         if( bHandleEOF && bRetVal )  /* if true, then write EOF */
         {
            char cEOF = HB_CHAR_EOF;
            hb_fileWrite( pFile, &cEOF, sizeof( char ), -1 );
         }

         hb_fileClose( pFile );
      }
   }

   return bRetVal;
}

HB_FUNC( HB_MEMOWRIT )
{
   hb_retl( hb_memowrit( HB_FALSE ) );
}

HB_FUNC( MEMOWRIT )
{
   hb_retl( hb_memowrit( HB_TRUE ) );
}
 

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 8:18 am
by Enrico Maria Giordano
As I wrote: useless things.

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 8:20 am
by Antonio Linares
I don't think this is useless at all:
Code: Select all  Expand view
        /* NOTE: CA-Cl*pper will add the EOF even if the write failed. [vszakats] */
         /* NOTE: CA-Cl*pper will not return .F. when the EOF could not be written. [vszakats] */
         if( bHandleEOF && bRetVal )  /* if true, then write EOF */
         {
            char cEOF = HB_CHAR_EOF;
            hb_fileWrite( pFile, &cEOF, sizeof( char ), -1 );
         }

It writes or it does not write the HB_CHAR_EOF. Most of times, such char is not needed at all, and that is truly useless. MemoWrit() always writes it.

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 8:30 am
by Enrico Maria Giordano
Write? We are speaking of MEMOREAD().

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 8:31 am
by Antonio Linares
In the above code both functions are used

Re: insert text into file text

PostPosted: Mon Jul 01, 2024 8:39 am
by Enrico Maria Giordano
The problem is MEMOREAD()/HB_MEMOREAD(). There would be no need to create a new function.