Solved

File access.

Posted on 2000-03-15
16
161 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 149
how to manage invalidate between two tvirtualstringtree in same form? 1 127
find a node in VST 2 70
Browsing a TTreeView in Delphi 5 6
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

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