Solved

File access.

Posted on 2000-03-15
16
158 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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

24 Experts available now in Live!

Get 1:1 Help Now