Solved

File access.

Posted on 2000-03-15
16
157 Views
Last Modified: 2010-04-04
Hi,
For example, strings are being continuously input from keyboard.  Is there a way which can store the strings into memory, and only save it into a file, say, strings.txt when the memory has accumulated 10 kbytes?  The strings being entered may contain all ASCII codes, ie. from 0 to 255.  Please don't suggest me to use TMemo or any other components.
Thanks in advance.
0
Comment
Question by:nyt
  • 5
  • 3
  • 2
  • +3
16 Comments
 

Expert Comment

by:johnwa
ID: 2618995
you can use TStream to do it.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2619006
TStringlist?

StringList1[0] := StringList1[0] + NewKey;
IF Length(StringList1[0]) > 10240
THEN
  //Save data

The Neil
0
 

Expert Comment

by:johnwa
ID: 2619012
you can just set all the items's Visible property of the popmenu  to false.
0
 

Expert Comment

by:mike_tipping
ID: 2619023
Sounds like using a string list would be fun (but since that's the underlying part of a TMemo it might not be what you want to do).

You can check the length of the entered struings by using length(tStringlist.text) or some such thing then when it exceeds you limit use TStringList.savetofile.

 
0
 

Author Comment

by:nyt
ID: 2619149
I prefer methods using TStream.  But can I have any sample codes?
0
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 2619157
You can use TFileStream.

var F : TFileStream;
begin
   F := TFileStream.Create('filename.ext',fmCreate);

   ....
   F.Write(YourKey,1);    
   ....

   F.Free;
end;

 You dont need care about buffering. It will buffering your input automatically. And you can append data to exsisting file:

......
   F := TFileStream.Create('filename.ext',fmOpenWrite);
   F.Seek(0,2); // go to the end of file

   ....
   F.Write(YourKey,1);    
   ....

   F.Free;
end;


Best regards,
Igor.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2619178
In the OnCreate method:

stlBuffer := TStringList.Create

In the KeyPress/Up/Down method:

stlBuffer[0] := stlBuffer[0] + <YourKey>;
IF Length(stlBuffer[0]) > 10240
THEN
BEGIN
  AssignFile(data_out);
  ReWrite(data_out);
  WRITELN(data_out, stlBuffer[0]);
  CloseFile(data_out);
  stlBuffer[0] := '';
END;

In the OnDestroy method:

stlBuffer.Free

You can also add a check to the OnDestroy method to save any outstanding key presses

The Neil
0
 

Expert Comment

by:mike_tipping
ID: 2619224
Wouldn't the problem with the FileStream be that it is saving the strings to disk all the time and not bothering about how full the memory part is.

I would do it as per TheNeil but append rather than rewrite.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:TheNeil
ID: 2619379
I used ReWrite as I read it as meaning that NYT wanted to just output 10Kb to each file. It's swings and roundabouts though

The Neil =:)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2622468
Why using a stringlist? Why not simply using a string?

var stlBuffer : string;

In the KeyPress/Up/Down method:

stlBuffer := stlBuffer + <YourKey>;
IF Length(stlBuffer) > 10240
THEN
BEGIN
  with TFileStream.Create(data_out, fmCreate) do
    try
      Write(pointer(stlBuffer)^, length(stlBuffer));
    finally Free end;
  stlBuffer := '';
END;

Regards, Madshi.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2622812
Madshi is right (as always). Why the hell did I think about using a stringlist? It's just a load of strings when you only need one - d'oh (you idiot Neil)

The Neil
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2622870
:-))
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2622875
All right, all right - don't start gloating

The Neil =;)
0
 

Author Comment

by:nyt
ID: 2635728
This question has a deletion request Pending
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2635775
This question no longer is pending deletion
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2635776
Any answer you has not satisfied?

Igor;)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now