RAD Studio VCL Reference
|
Writes Count bytes from the Buffer to the current position in the resource.
function Write(const Buffer; Count: Longint): Longint; override;
virtual __fastcall Longint Write(const Buffer, Longint Count);
Use Write to write Count bytes to the resource associated with the handle stream, starting at the current position. After writing to the resource, Write advances the current position by the number bytes written, and returns the number of bytes written.
All other data-writing methods of a handle stream (WriteBuffer, WriteComponent) call Write to do the actual writing.
C++ Examples:
/* This example reads any file and converts the encoding to UTF8. This example uses the TBytesStream to read the files into a TBytes' and also uses the auto_ptr construction to clean up created class instances. */ #include <tchar.h> #include <memory> //for STL auto_ptr class //--------------------------------------------------------------------------- #pragma argsused int _tmain(int argc, _TCHAR* argv[]) { // Sample to convert a file of any encoding to UTF8. TEncoding *LEncoding = NULL; std::auto_ptr<TFileStream> LFileStream(new TFileStream("..\\Sample.txt", fmOpenRead)); // Read file into buffer TBytes myBytes; std::auto_ptr<TBytesStream> myBytesStream(new TBytesStream(myBytes)); myBytesStream->CopyFrom(LFileStream.get(), LFileStream->Size); // Identify encoding and convert buffer to UTF8 int LOffset = TEncoding::GetBufferEncoding(myBytesStream->Bytes, LEncoding); if (LOffset == 0) return 1; // Unknown encoding, don't convert. myBytes = TEncoding::Convert(LEncoding, TEncoding::UTF8, myBytesStream->Bytes, LOffset, myBytesStream->Size-LOffset); // Create output file std::auto_ptr<TFileStream> DestFileStream(new TFileStream("..\\SampleUTF8.txt", fmCreate)); // Write UTF8 byte order mark and buffer to output file TBytes LByteOrderMark; LByteOrderMark = TEncoding::UTF8->GetPreamble(); // Grab preamble and write to destination DestFileStream->Write(&LByteOrderMark[0], LByteOrderMark.Length); // Write converted buffer DestFileStream->Write(&myBytes[0], myBytes.Length); return 0; }
Delphi Examples:
{ This example determines the encoding of a source file and then writes the file in any encoding chosen. Use this project to create files of any encoding. This example requires two text edits and a combobox. } procedure TForm1.Button1Click(Sender: TObject); var LBuffer: TBytes; LByteOrderMark: TBytes; LOffset: Integer; LEncoding, DestEncoding: TEncoding; LFileStream: TFileStream; EncodingArray: array[0..5] of TEncoding; begin LEncoding:= nil; EncodingArray[0]:= TEncoding.UTF8; EncodingArray[1]:= TEncoding.UTF7; EncodingArray[2]:= TEncoding.Unicode; EncodingArray[3]:= TEncoding.Default; EncodingArray[4]:= TEncoding.BigEndianUnicode; EncodingArray[5]:= TEncoding.ASCII; DestEncoding := EncodingArray[ComboBox1.ItemIndex]; LFileStream := TFileStream.Create(Edit1.Text, fmOpenRead); try // Read file into buffer SetLength(LBuffer, LFileStream.Size); // LFileStream.Read(LBuffer[0], Length(LBuffer)); LFileStream.ReadBuffer(Pointer(LBuffer)^, Length(LBuffer)); // Identify encoding and convert buffer to UTF8 LOffset := TEncoding.GetBufferEncoding(LBuffer, LEncoding); // for I := 0 to Length(LBuffer) - 1 do // Memo1.Lines.Add((LBuffer[I])); LBuffer := LEncoding.Convert(LEncoding, DestEncoding, LBuffer, LOffset, Length(LBuffer) - LOffset); finally LFileStream.Free; end; LFileStream := TFileStream.Create(Edit2.Text, fmCreate); try // Write an encoding byte order mark and buffer to output file LByteOrderMark := DestEncoding.GetPreamble; LFileStream.Write(LByteOrderMark[0], Length(LByteOrderMark)); LFileStream.Write(LBuffer[0], Length(LBuffer)); finally LFileStream.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); var StringList: TStrings; begin StringList := TStringList.Create; try with StringList do begin Add('TEncoding.UTF8'); Add('TEncoding.UTF7'); Add('TEncoding.Unicode'); Add('TEncoding.Default'); Add('TEncoding.BigEndianUnicode'); Add('TEncoding.ASCII'); end; with ComboBox1 do begin Items.Assign(StringList); ItemIndex := 0; end; finally StringList.free; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|