insert text into file text

Post Reply
User avatar
damianodec
Posts: 422
Joined: Wed Jun 06, 2007 2:58 pm
Location: Italia
Contact:

insert text into file text

Post 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
FiveWin for xHarbour 17.09 - Sep. 2017 - Embarcadero C++ 7.00 for Win32
FWH 64 for Harbour 19.06 (MSVC++) Jun. 2019 - Harbour 3.2.0dev (r1904111533)
Visual Studio 2019 - Pelles C V.8.00.60 (Win64)
User avatar
Antonio Linares
Site Admin
Posts: 42259
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: insert text into file text

Post by Antonio Linares »

Code: Select all | Expand

    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" )
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
damianodec
Posts: 422
Joined: Wed Jun 06, 2007 2:58 pm
Location: Italia
Contact:

Re: insert text into file text

Post 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)
FiveWin for xHarbour 17.09 - Sep. 2017 - Embarcadero C++ 7.00 for Win32
FWH 64 for Harbour 19.06 (MSVC++) Jun. 2019 - Harbour 3.2.0dev (r1904111533)
Visual Studio 2019 - Pelles C V.8.00.60 (Win64)
User avatar
Enrico Maria Giordano
Posts: 8728
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: insert text into file text

Post by Enrico Maria Giordano »

Use MEMOREAD() instead. The Harbour developers love to add useless things... :-(
User avatar
Antonio Linares
Site Admin
Posts: 42259
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: insert text into file text

Post 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

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 ) );
}
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42259
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: insert text into file text

Post by Antonio Linares »

I don't think this is useless at all:

Code: Select all | Expand

         /* 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.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42259
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: insert text into file text

Post by Antonio Linares »

In the above code both functions are used
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Enrico Maria Giordano
Posts: 8728
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: insert text into file text

Post by Enrico Maria Giordano »

The problem is MEMOREAD()/HB_MEMOREAD(). There would be no need to create a new function.
Post Reply