We help IT Professionals succeed at work.

File access.

nyt
nyt asked
on
181 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.
Comment
Watch Question

Commented:
you can use TStream to do it.

Commented:
TStringlist?

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

The Neil

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

 
nyt

Author

Commented:
I prefer methods using TStream.  But can I have any sample codes?
Senior developer
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
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
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.

Commented:
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 =:)

Commented:
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.

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

Commented:
:-))

Commented:
All right, all right - don't start gloating

The Neil =;)
nyt

Author

Commented:
This question has a deletion request Pending
Igor UL7AAjrSenior developer

Commented:
This question no longer is pending deletion
Igor UL7AAjrSenior developer

Commented:
Any answer you has not satisfied?

Igor;)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.