Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 343069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:51:47+00:00 2026-05-12T10:51:47+00:00

I’m using Delphi7 (non-unicode VCL), I need to store lots of WideStrings inside a

  • 0

I’m using Delphi7 (non-unicode VCL), I need to store lots of WideStrings inside a TFileStream. I can’t use TStringStream as the (wide)strings are mixed with binary data, the format is projected to speed up loading and writing the data … However I believe that current way I’m loading/writing the strings might be a bottleneck of my code …

currently I’m writing length of a string, then writing it char by char …
while loading, first I’m loading the length, then loading char by char …

So, what is the fastest way to save and load WideString to TFileStream?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:51:47+00:00Added an answer on May 12, 2026 at 10:51 am

    Rather than read and write one character at a time, read and write them all at once:

    procedure WriteWideString(const ws: WideString; stream: TStream);
    var
      nChars: LongInt;
    begin
      nChars := Length(ws);
      stream.WriteBuffer(nChars, SizeOf(nChars);
      if nChars > 0 then
        stream.WriteBuffer(ws[1], nChars * SizeOf(ws[1]));
    end;
    
    function ReadWideString(stream: TStream): WideString;
    var
      nChars: LongInt;
    begin
      stream.ReadBuffer(nChars, SizeOf(nChars));
      SetLength(Result, nChars);
      if nChars > 0 then
        stream.ReadBuffer(Result[1], nChars * SizeOf(Result[1]));
    end;
    

    Now, technically, since WideString is a Windows BSTR, it can contain an odd number of bytes. The Length function reads the number of bytes and divides by two, so it’s possible (although not likely) that the code above will cut off the last byte. You could use this code instead:

    procedure WriteWideString(const ws: WideString; stream: TStream);
    var
      nBytes: LongInt;
    begin
      nBytes := SysStringByteLen(Pointer(ws));
      stream.WriteBuffer(nBytes, SizeOf(nBytes));
      if nBytes > 0 then
        stream.WriteBuffer(Pointer(ws)^, nBytes);
    end;
    
    function ReadWideString(stream: TStream): WideString;
    var
      nBytes: LongInt;
      buffer: PAnsiChar;
    begin
      stream.ReadBuffer(nBytes, SizeOf(nBytes));
      if nBytes > 0 then begin
        GetMem(buffer, nBytes);
        try
          stream.ReadBuffer(buffer^, nBytes);
          Result := SysAllocStringByteLen(buffer, nBytes)
        finally
          FreeMem(buffer);
        end;
      end else
        Result := '';
    end;
    

    Inspired by Mghie’s answer, have replaced my Read and Write calls with ReadBuffer and WriteBuffer. The latter will raise exceptions if they are unable to read or write the requested number of bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.