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 8234337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:26:25+00:00 2026-06-07T18:26:25+00:00

I am trying to write and read a non-fixed string using TFileStream. I am

  • 0

I am trying to write and read a non-fixed string using TFileStream. I am getting an access violation error though. Here is my code:

// Saving a file
  (...)
  count:=p.Tags.Count; // Number of lines to save (Tags is a TStringList)
  FS.Write(count, SizeOf(integer));
  for j := 0 to p.Tags.Count-1 do
  begin
    str:=p.Tags.Strings[j];
    tmp:=Length(str)*SizeOf(char);
    FS.Write(tmp, SizeOf(Integer));
    FS.Write(str[1], Length(str)*SizeOf(char));
  end;

// Loading a file
  (...)
  p.Tags.Add('hoho'); // Check if Tags is created. This doesn't throw an error.
  Read(TagsCount, SizeOf(integer)); // Number of lines to read
  for j := 0 to TagsCount-1 do
  begin
    Read(len, SizeOf(Integer)); // length of this line of text
    SetLength(str, len); // don't know if I have to do this
    Read(str, len); // No error, but str has "inaccessible value" in watch list
    p.Tags.Add(str); // Throws error
  end;

The file seems to save just fine, when I open it with a hexeditor, I can find the right strings saved there, but loading is throwing errors.

Could you help me out?

  • 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-06-07T18:26:26+00:00Added an answer on June 7, 2026 at 6:26 pm

    You save the number of bytes, and that’s how many bytes you write. When you read the value, you treat it as the number of characters, and then read that many bytes. That won’t cause the problem you’re seeing now, though, since you’re making the buffer bigger than it needs to be as of Delphi 2009.

    The problem is that you’re reading into the string variable, not the string’s contents. You used str[1] when writing; do the same when reading. Otherwise, you’re overwriting the string reference that you allocated when you called SetLength.

    Read(nBytes, SizeOf(Integer));
    nChars := nBytes div SieOf(Char);
    SetLength(str, nChars);
    Read(str[1], nBytes);
    

    And yes, you do need to call SetLength. Read doesn’t know what its reading into, so it has no way of knowing that it needs to set the size to anything in advance.

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

Sidebar

Related Questions

I am trying to write some simple code which will read a text file
I have been trying to write some code in Scala to read a file
I'm using read/write locks on Linux and I've found that trying to upgrade a
I'm trying to write and then read a variable to a file. The variable
I was trying to make a Read\Write function for websocket but i've a problem...
i have been trying to write a program in python to read two text
I am trying to write a simple program in C# that will read data
I am trying to write a script in Perl to read all the icons
I'm trying to write a short function that will let me quickly read in
I am trying to write a small Mac command line app that can read

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.