🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8247

Re: TIniFiles

#8247
From: David Scheffel
Date: Fri, 05 Nov 2004 14:54
148 lines
6296 bytes
> But still, what's about my question?
> When I write I get an exception:
> Unable to write to D:\SpeedraTel\PROGRAMME\SpeedraTest.ini at 0x0

1) could be your not allowed to write to this file for some reson
2) could be a problem with the way windows writes to the ini file

possible causes:

case 1)
- an existing instance of your application is still in memory as cause of an 
ressource leak
- you have no permissions to write to this directory

case 2)
- use another class to write the data. One who writes directly to an file 
(w/o using the windows api). E.g. the TMemIniFile I've recommended before.

But actually I still think that this problem connected with the application 
design. TIniFile is used by millions of developers since about 10 years. So 
I don't think that one of the VCL classes the source of the problem anyway.

To be sure you could build an sample project with an timer to write or read 
from an TIniFile for hours and days. If you're using W9x you should also 
call "flush" (see help). If this will work then you're absolutely sure that 
the VCL class works properbly and your problem lies elsewhere.

If I might give you an hint: "Get rid of this crappy code to avoid these 
weird errors". A good concept as well as readable and maintainable source 
code always pays for it self! If you lose the outline you lose control.

Here is the TIniFile code example from the BCB help:

void __fastcall TForm1::TForm1(TObject *Sender)

{
   TIniFile *ini;
   ini = new TIniFile(
      ChangeFileExt( Application->ExeName, ".INI" ) );
   Top     =  ini->ReadInteger( "Form", "Top", 100 );
   Left    =  ini->ReadInteger( "Form", "Left", 100 );
   Caption =  ini->ReadString( "Form", "Caption",
                               "Default Caption" );
   ini->ReadBool( "Form", "InitMax", false ) ?
         WindowState = wsMaximized :
         WindowState = wsNormal;
   delete ini;

}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   TIniFile *ini;
   ini = new TIniFile(ChangeFileExt( Application->ExeName, ".INI" ) );
   ini->WriteInteger( "Form", "Top", Top );
   ini->WriteInteger( "Form", "Left", Left );
   ini->WriteString ( "Form", "Caption", Caption );
   ini->WriteBool   ( "Form", "InitMax",
                       WindowState == wsMaximized );
   delete ini;
}

>
> This procedure TestINI() is just to find out when the problem in the 
> program occurs. Then it starts the program new. But that's not a real 
> solution.
>
> You have any idea why I get an exception? What can be the reason?
>
> Thank you
> Lothar
>
>
> David Scheffel schrieb:
>
>>Hi Lothar,
>>
>>I don't feel like your application is well designed right now. There are a 
>>lot of things that should be improved basically. E.g. the following:
>>
>>1.) don't use static char arrays, use AnsiString instead
>>2.) since sprintf is unable to throw exceptions you should use Format 
>>instead
>>3.) you don't need to check if pIniFile is null after you have constructed 
>>it! pIniFile is NOT an handle to an IniFile!! It is an pointer that points 
>>to an instance of TIniFile. If the ctor of TIniFile runs into an error 
>>it'll throw an Exception anyway
>>4.) avoid the use of timers unless it's really necessary. You should read 
>>the settings when the application is constructed. If you have only a 
>>single form or MDI Main Form you could read the settings the the ctor of 
>>the form
>>
>>Further more I'd recommend you to read at least one of these books: 
>>"Borland C++Builder 4 Unleashed", "C++Builder 6 Developer's Guide". If 
>>you're still unhappy with TIniFile you might have o look at TMemIniFile 
>>which works independant from windows and is much faster since it hold the 
>>data in the memory.
>>
>>regards
>>//David
>>
>>
>>>void CPtimer::TestINI()
>>>{
>>>  TIniFile *pIniFile;
>>>  char  DisplayStr[200];
>>>
>>>  if (!TestINIok) return;
>>>try {
>>>  try {
>>>     pIniFile = new 
>>> TIniFile(ExtractFilePath(ParamStr(0))+"SpeedraTest.ini");
>>>     if (pIniFile) {
>>>       pIniFile->WriteDateTime("Test",TestINIstart, Now());
>>>       sprintf(DisplayStr,"TestINI ok %d",pIniFile);
>>>       Display(DisplayKanalx,DisplayStr);
>>>     } else {
>>>       DisplaySystemError(DisplayKanalx,"TestINI Fehler");
>>>       TestINIok=false;
>>>       Form1->EndeundNeustartwennohneAnruf->Checked= true;   // new start 
>>> of program
>>>     }
>>>  } catch(const Exception& e) {
>>>       DisplaySystemError(DisplayKanalx,"Exception TestINI");
>>>       sprintf(DisplayStr,"!!!Exception: %s at 
>>> 0x%X",NoSteuer(e.Message).c_str(),ExceptAddr());
>>>       Display(DisplayKanalx,DisplayStr);
>>>       TestINIok=false;
>>>       Form1->EndeundNeustartwennohneAnruf->Checked= true;   // new start 
>>> of program
>>>  }
>>>
>>>  delete pIniFile;
>>>} catch(const Exception& e) {
>>>}
>>>}
>>>
>>>
>>>David Scheffel schrieb:
>>>
>>>
>>>>Could you post the source code?
>>>>
>>>>
>>>>
>>
>>
>>
> 
Technical Details
Message-ID: <418b944e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!t-online.de!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4189f894$1@newsgroups.borland.com> <418a15ad$1@newsgroups.borland.com> <418a22f5$2@newsgroups.borland.com> <418b6cf9$1@newsgroups.borland.com> <418b818c$3@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,db95008868615205
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "David Scheffel" <noreply@spammy.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4189f894$1@newsgroups.borland.com> <418a15ad$1@newsgroups.borland.com> <418a22f5$2@newsgroups.borland.com> <418b6cf9$1@newsgroups.borland.com> <418b818c$3@newsgroups.borland.com>
Subject: Re: TIniFiles
Date: Fri, 5 Nov 2004 15:54:56 +0100
Lines: 150
Organization: Novaris Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 212.227.35.73
Message-ID: <418b944e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1099666510 212.227.35.73 (5 Nov 2004 06:55:10 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!t-online.de!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail