saving text from a file into memory

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?

thanx!
urifAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
MadshiConnect With a Mentor Commented:
I'm using this usually, which load the file into a simple Delphi dynamic string:

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

Regards, Madshi.
0
 
kretzschmarCommented:
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
0
 
kretzschmarCommented:
hi madshi ;-)
0
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.

 
MadshiCommented:
hi meikl...  (-:
0
 
urifAuthor Commented:
To Madshi:

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

furthermore, where do you return that string?
0
 
AvonWyssCommented:
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);
var
     B,E: ^Byte;
     Temp: Byte;
begin
     if Count>1 then begin
          E:=@Data;
          B:=E;
          Inc(E,Count-1);
          Count:=Count shr 1;
          while Count>0 do begin
               Temp:=B^;
               B^:=E^;
               E^:=Temp;
               Inc(B);
               Dec(E);
               Dec(Count);
          end;
     end;
end;

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

     SwapChar(YourString[1],Length(YourString));

Easy, isn't it...
0
 
urifAuthor Commented:
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!
0
 
MadshiCommented:
>> 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;
begin
  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.
0
 
urifAuthor Commented:
thx again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.