saving text from a file into memory

Posted on 2001-06-26
Last Modified: 2012-05-04
Hello all,

I have a console application that needs to read a file, get the text inside and save it into a memory stream or something the like, i need this because i will passing that text to a function and later i will show the resulting text on the screen. So this is what i need:

1. read from file.txt
2. fill a buffer with that text
3. pass the buffer to a function (this function will reverse the text)
4. show the resulting text on the screen.

any ideas how to build such a buffer?

Question by:urif
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
LVL 27

Expert Comment

ID: 6230133
what about to use a tmemorystream?

it provides a loadfromfile-method,
a pointer and a size proberty

or you can even use a string
loading your text in a tsringlist and forward its text-proeprty
LVL 20

Accepted Solution

Madshi earned 50 total points
ID: 6230172
I'm using this usually, which load the file into a simple Delphi dynamic string:

function LoadStrFromFile(filename: string; var str: string) : boolean;
  result := false;
    if GetFileAttributes(pchar(filename)) <> INVALID_HANDLE_VALUE then
      with TFileStream.Create(filename, fmOpenRead or fmShareDenyNone) do begin
        SetLength(str, Size);
        Read(pointer(str)^, Size);
        result := true;
  except end;

Regards, Madshi.
LVL 27

Expert Comment

ID: 6230187
hi madshi ;-)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 20

Expert Comment

ID: 6230201
hi meikl...  (-:

Author Comment

ID: 6230282
To Madshi:

Thx, are you sure the file text will fit into a normal string?

furthermore, where do you return that string?
LVL 14

Expert Comment

ID: 6230682
urif, since Delphi 2, Strings can be any size up to about 2GB. So if your text file is larger, well... hm... ;-)

To reverse a string, you could use this code:

procedure SwapChar(var Data; Count: Integer);
     B,E: ^Byte;
     Temp: Byte;
     if Count>1 then begin
          Count:=Count shr 1;
          while Count>0 do begin

and then after loading the text into YourString using Madshi's code:


Easy, isn't it...

Author Comment

ID: 6230798
sorry about my naivity, i'm a c programmer (in linux/unix!!!) i'm new to this windows, my pascal is the pascal learned in college

thx for your help, i really apreciated!
LVL 20

Expert Comment

ID: 6230857
>> Thx, are you sure the file text will fit into a normal string?

As AvonWyss already pointed out, a string can theoretically hold up to 2 GB data. But there is a limit, namely the available RAM + swap file. Generally you can easily load text files up to 1 MB into memory, if the text files are much larger, you should think about loading it in in thunks.

>> furthermore, where do you return that string?

It works like this:

var strVar : string;
  if LoadStrFromFile('c:\autoexec.bat', strVar) then begin
    // here you have the file in the "strVar"
  end else
    ShowMessage('loading failed...');

>> sorry about my naivity, i'm a c programmer

No problem...   (-:

Regards, Madshi.

Author Comment

ID: 6230885
thx again

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question