Solved

saving text from a file into memory

Posted on 2001-06-26
9
127 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 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;
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 119
Help on project with Soap 10 53
Run video youtube webbrowse 10 61
Delphi: sending SMS on android platform 1 18
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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