Solved

Smooth, and I mean smoooth,  File I/O

Posted on 1998-06-19
9
196 Views
Last Modified: 2010-04-04
If I wanted to be able to write / read information to a text file, how would I do it?  An important feature would be the ability to add / delete items and have the size of the file shrink / grow automatically.

I have used INI files for a lot of my storage up until now, and they are okay, but not really what I want.

I've read a little bit here and there about Streams and this might be what I'm looking for.

If someone could give me a clear, concise code example that demonstrates how to read info, modify that info (add, edit delete), the write that info out to the text file, that would be super.  

I am not interested in using INI files or a database.  Streams, Linked Lists, Custom data structures, etc are more what I'm looking for.

Thanks for helping a beginner.

Tom
0
Comment
Question by:knowlton
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Author Comment

by:knowlton
ID: 1354385
Here's some more info to help you out:

Here's what a data structure I would use might look like:

Type
   DEFFile = record
      Tag,
      FileName,
      Group,
      Width,
      Height,
      AnimationRate,
      Variations : string;
   end;

During the OnCreate or some other appropriate time, I would load the file information into this data structure.  I would then change the information, and write the information back out to the text file it came from.
0
 

Expert Comment

by:smico
ID: 1354386
If you are looking for a "fast food", than TStringList could solve your problem.
..........
begin.
myStringList:=TStringList.create;
with myStringList do
 begin
 LoadFromFile('c:\myTextFile.txt');
 Add('Add one line of text');
 Insert(0,'Insert one line');
 Strings[0]:='Edit Line'+Strings[0]+' and add ketchup';
 Delete(0);  //delete this line
 SaveToFile('c:\myTextFile.txt');
 end;
.......

Regards
Mico
0
 
LVL 1

Expert Comment

by:Greedy
ID: 1354387
well if you need to read and write to a "TextFile" I would do this

To Read it
var
  MyFile : TextFile
  CluckyAssStringList : TStringList;
  TempString : string;

begin
  CluckyAssStringList := TStringList.Create;
  AssignFile(MyFile, <Path To My Fat File>);
  Reset(MyFile);
  while not EOF(MyFile) do
  begin
    Readln(MyFile, TempString);
    CluckAssStringList.Add(TempString);
  end;
 

Then Once you have the strings into the StringList you can sort it insert other strings delete...whatever

then to write it do this...not compleat because I don't have too much time

begin
  Rewrite(MyFile)
  For I := 0 to CluckyAssStringList.Count -1 do
  begin
    writeln(MyFile, CluckyAssStringList[I]);
  end;
  CloseFile(MyFile);

    You get the Idea...

These functions may come in handy for you too

ExtractFilePath(Application.ExeName)
FileExists(<your file name>)

Take a look at
http://www.inprise.com/devsupport/delphi/ti_list/TI2719.html


0
 
LVL 1

Expert Comment

by:Greedy
ID: 1354388
acctualy I like smicro's LoadFromFile and SaveToFile Methods of TStrings better...less code.  but I just thought I'd type that code in so that you can "get a feal"  
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:bcrotaz
ID: 1354389
Another (more Delphi) way is to use an object rather than a record:

type
  TMyRecord=class(TPersistent)
    private
      FMyField1: Integer;
      FMyField2: String;
    published  // all published properties auto handled by TPersistent
      property MyField1: Integer read FMyField1 write FMyField1;
      property MyField2: Integer read FMyField2 write FMyField2;
    end;

  Now just call MyRecord.SaveToStream and MyRecord.LoadFromStream.


To create a new filestream:

  MyFile:=TFileStream.Create('c:\Text.txt',cmOpenReadWrite);
  try
    // read or write records here
  finally // make sure it happens
    MyFile.Free;
  end;

Actually, streams make this sort of stuff easy!

Bryan


0
 
LVL 5

Author Comment

by:knowlton
ID: 1354390
BCROTAZ:

This object thing seems to be what I'm looking for, but how do I add / retrieve items from the object, exactly?
0
 
LVL 5

Author Comment

by:knowlton
ID: 1354391
I like everyone's answers, and they have been helpful, to a point, but I need it really spelled out, using the model I gave in my second posting as a guide.  Up until now, I don't consider my question to be answered.

I need to be able to write to and read from a file...I need the complete source code that would do this, not an example that does it halfway and then assumes I can finish the rest.

Thanks everyone.
0
 
LVL 5

Author Comment

by:knowlton
ID: 1354392
smico:

Well, this has pretty much fizzled out, I guess.  You're answer was actually the most usefull to me.  Resubmit your comment as an answer.  You get the points!

Tom
0
 

Accepted Solution

by:
smico earned 30 total points
ID: 1354393
If you are looking for a "fast food", than TStringList could solve your problem.
      ..........
      begin.
      myStringList:=TStringList.create;
      with myStringList do
       begin
       LoadFromFile('c:\myTextFile.txt');
       Add('Add one line of text');
       Insert(0,'Insert one line');
       Strings[0]:='Edit Line'+Strings[0]+' and add ketchup';
       Delete(0);  //delete this line
       SaveToFile('c:\myTextFile.txt');
       end;
      .......

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
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, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

12 Experts available now in Live!

Get 1:1 Help Now