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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:13:14+00:00 2026-05-16T08:13:14+00:00

While porting some code from Delphi 7 to Delphi 2010 I was rewriting my

  • 0

While porting some code from Delphi 7 to Delphi 2010 I was rewriting my LoadTextFromFile() function.

function LoadTextFromFile(const aFullFileName: string): string;
var
  lBuffer:     TBytes;
  lEncoding:   TEncoding;
  lFileStream: TFileStream;
  lSize:       Integer;

begin

  if not FileExists(aFullFileName) then
  begin
    raise Exception.Create('File "' + aFullFileName + '" not found.');
  end;

  lFileStream := TFileStream.Create(aFullFileName, fmOpenRead + fmShareDenyNone);
  try

    if lFileStream.Size <= 0 then
    begin
      Result := '';
    end
    else
    begin

      lSize := lFileStream.Size - lFileStream.Position;

      SetLength(lBuffer, lSize);

      // Read file into TBytes buffer
      lFileStream.Read(lBuffer[0], lSize);

      // Read encoding from buffer
      TEncoding.GetBufferEncoding(lBuffer, lEncoding);

      // Get string from buffer
      Result := lEncoding.GetString(lBuffer);

    end;

  finally
    lFileStream.Free;
  end;

end;

When a thought was hitting my head: there must be something like this in the standard library. Many users want to read a text file into a string, but I could not find such a standard function. The closest I came was using TStringlist to load text. But A) creating a TStringlist looks unnecessary and B) I don’t want to suffer the overhead from TStringlist.

Question: is there a standard LoadTextFromFile function in Delphi 2010?

  • 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-16T08:13:15+00:00Added an answer on May 16, 2026 at 8:13 am

    Yes exist one function like that in Delphi 2010, is called ReadAllText and is part of the TFile record declarated in the IOUtils unit.

    Check this declaration

    class function ReadAllText(const Path: string): string; overload; inline; static;
    class function ReadAllText(const Path: string; const Encoding: TEncoding): string; overload; inline; static;
    

    see this sample

    program Project80;
    
    {$APPTYPE CONSOLE}
    
    uses
      IOUtils,
      SysUtils;
    Var
     Content : string;
    begin
      try
        Content:=TFile.ReadAllText('C:\Downloads\test.css'); //load the text of the file in a single line of code
        //Content:=TFile.ReadAllText('C:\Downloads\test.css',TEncoding.UTF8); //you can an encoding too.
        Writeln(Content);
        Readln;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the process of porting some code from Objective C to C++. I'm
I am doing some cross-platform work, porting code from Linux to Win32, but when
I'm porting some code I prototyped in python over to flash and while actionscript
I'm just porting some code over from .net to Java. I've been using the
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
After porting a code segment from Windows to Mac OS X, I found it
So, I am writing some OpenCV C++ code and I am having trouble porting
In my java code,I am retrieving some multibyte data from database and making some
I am porting some Unix code to Windows which redirects stderr and stdout to
I'm moving some code over to GCC 4.7 (from 4.6) and ran into a

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.