FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour insert text into file text
Posts: 434
Joined: Wed Jun 06, 2007 02:58 PM
insert text into file text
Posted: Fri Jun 28, 2024 07:55 AM

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 24.02 - Feb. 2024 - Embarcadero C++ 7.60 for Win32 Copyright (c) 1993-2023

FWH 64 for Harbour 19.06 (MSVC++) Jun. 2019 - Harbour 3.2.0dev (r1904111533)

Visual Studio 2019 - Pelles C V.8.00.60 (Win64)
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: insert text into file text
Posted: Fri Jun 28, 2024 09:03 AM
Code (fw): Select all Collapse
    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
Posts: 434
Joined: Wed Jun 06, 2007 02:58 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 06:13 AM

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 24.02 - Feb. 2024 - Embarcadero C++ 7.60 for Win32 Copyright (c) 1993-2023

FWH 64 for Harbour 19.06 (MSVC++) Jun. 2019 - Harbour 3.2.0dev (r1904111533)

Visual Studio 2019 - Pelles C V.8.00.60 (Win64)
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 07:33 AM
Use MEMOREAD() instead. The Harbour developers love to add useless things... :-(
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 08:16 AM
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 (fw): Select all Collapse
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
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 08:18 AM

As I wrote: useless things.

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 08:20 AM
I don't think this is useless at all:
Code (fw): Select all Collapse
         /* 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
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 08:30 AM

Write? We are speaking of MEMOREAD().

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 08:31 AM

In the above code both functions are used

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: insert text into file text
Posted: Mon Jul 01, 2024 08:39 AM

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

Continue the discussion