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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

File access.

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
nyt
Asked:
nyt
  • 5
  • 3
  • 2
  • +3
1 Solution
 
johnwaCommented:
you can use TStream to do it.
0
 
TheNeilCommented:
TStringlist?

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

The Neil
0
 
johnwaCommented:
you can just set all the items's Visible property of the popmenu  to false.
0
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!

 
mike_tippingCommented:
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
 
nytAuthor Commented:
I prefer methods using TStream.  But can I have any sample codes?
0
 
ITugayCommented:
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
 
TheNeilCommented:
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
 
mike_tippingCommented:
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
 
TheNeilCommented:
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
 
MadshiCommented:
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
 
TheNeilCommented:
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
 
MadshiCommented:
:-))
0
 
TheNeilCommented:
All right, all right - don't start gloating

The Neil =;)
0
 
nytAuthor Commented:
This question has a deletion request Pending
0
 
ITugayCommented:
This question no longer is pending deletion
0
 
ITugayCommented:
Any answer you has not satisfied?

Igor;)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now