Board index » delphi » INI file writing/reading problem

INI file writing/reading problem

I have trouble when writing an INI file and immediately reading it
again. I seem to be too fast ?!

My program sample looks somwhow like that:

procedure WriteIntoIni;         {creates just an inifile}
Var ini:TIniFile;
begin
  ini:=TIniFile.Create('C:\2.ini');
  ini.WriteString('Section1','Variable1','   12345 6789 abcdefg');
  ini.WriteString('Section1','Variable2','Hello  There  INIFile 123');
  ini.WriteString('LastSection','Lastvalue','End Of File.');
  ini.Free;
end;

procedure CodeNow;              {'copies' the inifile into another file and }
                                { manipulates the bytes}
Var b:Byte;                    
    fin,fout:File Of Byte;
begin
  AssignFile(fin,'C:\inifile.ini');
  AssignFile(fout,'C:\outfile.txt');
  Reset(fin);
  ReWrite(fout);
  While NOT(EoF(fin)) Do
  Begin
    Read(fin,b);
    b:=255-b;           {<<== that would be my simple coding}
    Write(fout,b);
  End;
  CloseFile(fin);
  CloseFile(fout);                      
end;

The trouble is: the routines function correctly when executed with some
time between calling writing and coding; when executed immediately after
another the filesize of the original inifile reported by delphi is
approximately 49 and the destination file contains just crashed bytes.

The problem occurs on a 386/40 and on a AMD K6/300 (--> doesn't depend
on machine I think)
BUT: with code compiled with Delphi 1.02 everything is alright it's just
with Delphi 2 and 3.

Has anyone seen such before ? Or please tell me, if my program is just
trash.
(I know that the coding routine isn't the best way to implement but I
think it is the safest)

Thank You Everyone. Harald Kleiner (e9625...@student.tuwien.ac.at)

 

Re:INI file writing/reading problem


Quote
Harald Kleiner wrote:
> I have trouble when writing an INI file and immediately reading it
> again. I seem to be too fast ?!

Windows has special handling for INI files and caches the file
internally (only writing it when it feels like it, which may take a
second or two).

Some rules you may like to live by:

#  If you write the INI file using Tinifile, read it using Tinifile (or
the WinAPI calls, which are not too bad).

#  Don't use INI files to synchronise programs over a network, where
timing is critical.  (and yes, I know you didn't ask this - I just threw
it in)

#  If you are polling an INI file to see if someone else has changed it,
don't do it continually, or it may take a long, long time.  Leave a nice
break between calls, say 100ms.

Andrew
--
Andrew Gabb
 email: ag...@tpgi.com.au
 phone: +61 8 8342-1021
   fax: +61 8 8269-3280
Technology Australasia's website is at http://www.taa.com.au
-----

Re:INI file writing/reading problem


Quote
Harald Kleiner wrote:
> I have trouble when writing an INI file and immediately reading it
> again. I seem to be too fast ?!

> My program sample looks somwhow like that:

> procedure WriteIntoIni;         {creates just an inifile}
> Var ini:TIniFile;
> begin
>   ini:=TIniFile.Create('C:\2.ini');
>   ini.WriteString('Section1','Variable1','   12345 6789 abcdefg');
>   ini.WriteString('Section1','Variable2','Hello  There  INIFile 123');
>   ini.WriteString('LastSection','Lastvalue','End Of File.');
>   ini.Free;
> end;

You could try ending this procedure with Application.ProcessMessages togive
Windows tiem to sort itself out !

Quote
> The trouble is: the routines function correctly when executed with some
> time between calling writing and coding; when executed immediately after
> another the filesize of the original inifile reported by delphi is
> approximately 49 and the destination file contains just crashed bytes.

HTH

Ray Cramer
R...@Pol.ac.uk

Other Threads