Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

saving text from a file into memory

Posted on 2001-06-26
9
Medium Priority
?
131 Views
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?

thanx!
0
Comment
Question by:urif
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
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
0
 
LVL 20

Accepted Solution

by:
Madshi earned 200 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;
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6230187
hi madshi ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Expert Comment

by:Madshi
ID: 6230201
hi meikl...  (-:
0
 

Author Comment

by:urif
ID: 6230282
To Madshi:

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

furthermore, where do you return that string?
0
 
LVL 14

Expert Comment

by:AvonWyss
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);
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
 

Author Comment

by:urif
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!
0
 
LVL 20

Expert Comment

by:Madshi
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;
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
 

Author Comment

by:urif
ID: 6230885
thx again
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

963 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