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

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

Smooth, and I mean smoooth, File I/O

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
Tom Knowlton
Asked:
Tom Knowlton
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Tom KnowltonWeb developerAuthor Commented:
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
 
smicoCommented:
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
 
GreedyCommented:
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
Independent Software Vendors: 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!

 
GreedyCommented:
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
 
bcrotazCommented:
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
 
Tom KnowltonWeb developerAuthor Commented:
BCROTAZ:

This object thing seems to be what I'm looking for, but how do I add / retrieve items from the object, exactly?
0
 
Tom KnowltonWeb developerAuthor Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
smicoCommented:
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

Industry Leaders: 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!

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