insert text into file text

insert text into file text

Postby damianodec » Fri Jun 28, 2024 7: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 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
damianodec
 
Posts: 419
Joined: Wed Jun 06, 2007 2:58 pm
Location: Italia

Re: insert text into file text

Postby Antonio Linares » Fri Jun 28, 2024 9:03 am

Code: Select all  Expand view  RUN
   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
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: insert text into file text

Postby damianodec » Mon Jul 01, 2024 6: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 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
damianodec
 
Posts: 419
Joined: Wed Jun 06, 2007 2:58 pm
Location: Italia

Re: insert text into file text

Postby Enrico Maria Giordano » Mon Jul 01, 2024 7:33 am

Use MEMOREAD() instead. The Harbour developers love to add useless things... :-(
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: insert text into file text

Postby Antonio Linares » Mon Jul 01, 2024 8: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: Select all  Expand view  RUN
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: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Re: insert text into file text

Postby Antonio Linares » Mon Jul 01, 2024 8:20 am

I don't think this is useless at all:
Code: Select all  Expand view  RUN
        /* 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: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: insert text into file text

Postby Enrico Maria Giordano » Mon Jul 01, 2024 8:30 am

Write? We are speaking of MEMOREAD().
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: insert text into file text

Postby Antonio Linares » Mon Jul 01, 2024 8:31 am

In the above code both functions are used
regards, saludos

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

Re: insert text into file text

Postby Enrico Maria Giordano » Mon Jul 01, 2024 8:39 am

The problem is MEMOREAD()/HB_MEMOREAD(). There would be no need to create a new function.
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 30 guests