?
Solved

Smooth, and I mean smoooth,  File I/O

Posted on 1998-06-19
9
Medium Priority
?
205 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 120 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

719 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