Solved

Smooth, and I mean smoooth,  File I/O

Posted on 1998-06-19
9
200 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 118
Delphi XE10 Round Image 2 189
PHP preg_replace code convert to Delphi 14 80
Firemonkey Tcameracomponent resultion 1 22
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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