Mega Search
23.2 Million


Sign Up

Make a donation  
SaveToFile + append  
News Group: embarcadero.public.delphi.nativeapi

Hi

I'm not shure to be in the right section...

I have this code :

 try
             stream.WriteBuffer(bytesData[0], length(bytesData));
             stream.SaveToFile('FileNameAndDirectory');
finally
             stream.Free;

Everything goes well except the fact that it doesn't append to file
it rewrites the file everytimes...

Can someone tell me how to append binary?

Thanks

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 4:33 PM EST
From: Pierre Zarzour
 
Re: SaveToFile + append  
News Group: embarcadero.public.delphi.nativeapi
Remy wrote:

> Basically yes, though the directory separator can be different.  Some
> platforms use '\', some use '/' instead.  You can use the
> TPath.DirectorySeparatorChar property to figure it out:
> 
> http://docwiki.embarcadero.com/Libraries/XE7/en/System.IOUtils.TPath.DirectorySeparatorChar
> 
> Or better, use TPath.Combine() to create your path strings:
> 
> http://docwiki.embarcadero.com/Libraries/XE7/en/System.IOUtils.TPath.Combine

Also, there are several other methods of TPath for locating special folders 
on each platform:

http://docwiki.embarcadero.com/RADStudio/XE7/en/Standard_RTL_Path_Functions_across_the_Supported_Target_Platforms

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 6:21 PM EST
From: Remy Lebeau (TeamB)
 
Re: SaveToFile + append  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> What do I write for ipod, android, ipad and mac instead of
> 'c:\directory\fileName'
> 
> is it the same thing but only the directory is changing?

Basically yes, though the directory separator can be different.  Some platforms 
use '\', some use '/' instead.  You can use the TPath.DirectorySeparatorChar 
property to figure it out:

http://docwiki.embarcadero.com/Libraries/XE7/en/System.IOUtils.TPath.DirectorySeparatorChar

Or better, use TPath.Combine() to create your path strings:

http://docwiki.embarcadero.com/Libraries/XE7/en/System.IOUtils.TPath.Combine

> Or maybe all this fileStream won't work in all the situations?

Yes, it will work just fine.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 6:18 PM EST
From: Remy Lebeau (TeamB)
 
Re: SaveToFile + append  
News Group: embarcadero.public.delphi.nativeapi
Works just fine!!!


While I have you...

What do I write for ipod, android, ipad and mac instead of 'c:\directory\fileName'

is it the same thing but only the directory is changing?

Or maybe all this fileStream won't work in all the situations?

Thanks  :)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 5:07 PM EST
From: Pierre Zarzour
 
Re: SaveToFile + append  
News Group: embarcadero.public.delphi.nativeapi
Pierre wrote:

> Everything goes well except the fact that it doesn't append to
> file it rewrites the file everytimes...

That is how all SaveToFile() methods are designed to work.  They always create 
a new file.

> Can someone tell me how to append binary?

Use SaveToStream() instead to save the stream to a separate TFileStream, eg:

{code}
stream := ...;
try
  stream.WriteBuffer(bytesData[0], length(bytesData));
  stream.Position := 0;
  fstream := TFileStream.Create('FileNameAndDirectory', fmOpenReadWrite or 
fmShareDenyWrite);
  try
    fStream.Seek(0, soEnd);
    stream.SaveToStream(fStream);
  finally
    fstream.Free;
  end;
finally
  stream.Free;
end;
{code}

In which case, you could just eliminate your original stream altogether and 
write directly to the TFileStream instead:

{code}
stream := TFileStream.Create('FileNameAndDirectory', fmOpenReadWrite or fmShareDenyWrite);
try
  stream.Seek(0, soEnd);
  stream.WriteBuffer(bytesData[0], length(bytesData));
finally
  stream.Free;
end;
{code}

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2015, at 4:58 PM EST
From: Remy Lebeau (TeamB)