Solved

Smooth, and I mean smoooth,  File I/O

Posted on 1998-06-19
9
201 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Author Comment

by:Tom 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
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!

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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