Recent

Author Topic: Can I edit a text file or do I have to delete it and recreate it?  (Read 9448 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 276
Can I edit a text file or do I have to delete it and recreate it?
« on: September 01, 2010, 03:29:36 pm »
I'm using a straight text ini file to set defaults in a program I'm writing and I want to be able to allow the user to change the defaults.  I have the code in place to change the running variables that effect the changes but I'm not sure the best way to go about getting those changes back into the ini file.  Is there a way I can read in a line, change it, and write it back out or will I have to delete the original ini file and recreate it each time a change is made?

Bill

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1926

wpflum

  • Sr. Member
  • ****
  • Posts: 276
Re: Can I edit a text file or do I have to delete it and recreate it?
« Reply #2 on: September 01, 2010, 06:11:30 pm »
I had looked at that briefly but I thought I saw someplace that it was based on a windows ini format not a straight text format?  Right now I have what I need working but I still might look at the tinifile include.

Thanks

bobc

  • New Member
  • *
  • Posts: 41
Re: Can I edit a text file or do I have to delete it and recreate it?
« Reply #3 on: September 01, 2010, 08:09:45 pm »
Is there a way I can read in a line, change it, and write it back out or will I have to delete the original ini file and recreate it each time a change is made?
There is not really a practical way to rewrite parts of a text file, unless for example you use a fixed line length and pad with spaces. You don't need to delete the file though, you can reopen it with write access and write the new data.

wpflum

  • Sr. Member
  • ****
  • Posts: 276
Re: Can I edit a text file or do I have to delete it and recreate it?
« Reply #4 on: September 01, 2010, 08:14:35 pm »
I found the 'rewrite' function and that is what I'm using right now and it seems to be working.  I would like to look into th tinifile at some point just to see if I can shorten the code up.

cdbc

  • Hero Member
  • *****
  • Posts: 707
    • http://www.cdbc.dk
Re: Can I edit a text file or do I have to delete it and recreate it?
« Reply #5 on: September 04, 2010, 10:09:14 am »
Hi

Open it with a 'TFilestream' in 'readwrite' mode...

Then you can alter the individual bytes as you please...

hth - Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6


bobc

  • New Member
  • *
  • Posts: 41
Re: Can I edit a text file or do I have to delete it and recreate it?
« Reply #7 on: September 04, 2010, 10:43:06 am »
Most of the time I need to load/save simple config values for applications, so I wrote a wrapper object for TIniFile which handles a lot of generic detail. I now add a TSettings object to a form, and have one line to load the ini file, and access string elements. Recently it grew to include XML support and the ability to associate with controls on the form, load/save standard window settings. It was originally written for Delphi, it's now pretty messy and needs rewriting, but I wonder if there are any units in Lazarus library for handling application config?

Code: [Select]
public
   AppSettings: TSettings;
...
begin
   // get the stored settings, e.g. on FormCreate
   AppSettings := TSettings.Create ('myapp');
   AppSettings.Load;

   MyValue := settings ['a_value'];
...

   // store settings, e.g. on Close
   settings ['a_value'] := 'new value';
   settings.Save;


theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1926
Re: Can I edit a text file or do I have to delete it and recreate it?
« Reply #8 on: September 04, 2010, 11:24:32 am »
but I wonder if there are any units in Lazarus library for handling application config?

TXMLPropStorage or TIniPropStorage in the Misc Tab.
http://wiki.lazarus.freepascal.org/TXMLPropStorage

captian jaster

  • Guest
Re: Can I edit a text file or do I have to delete it and recreate it?
« Reply #9 on: September 04, 2010, 09:55:07 pm »
I've said before I find it far more simpler to over write the file when  I change something. INI files are perfect for when doing this.
http://wiki.lazarus.freepascal.org/Using_INI_Files
The prop storage is fine. This is just what I prefer when working with settings.

 

TinyPortal © 2005-2018