Recent

Author Topic: Rewrite(xxx) causes "Access denied"  (Read 17509 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19166
  • Glad to be alive.
Re: Rewrite(xxx) causes "Access denied"
« Reply #15 on: June 13, 2017, 04:56:29 pm »
@rvk
@wp

I think you both forgot something. I think it needs to be:
Code: Pascal  [Select][+][-]
  1. type
  2.   TDatabase = packed record //!!! alignment is not a given value here.
  3.     ID: Integer;
  4.     DBName: String[120]; // or a maximum string-size
  5.   end;
Both alignment and the size of integer are mode dependent. Alignment even more so.
If you use a record for file IO it should ALWAYS be a packed record.

Also note that it would be better to use an un-even string size (shortstring[255]) to obtain an even record length, which is more efficient. Or an even array of AnsiChar. Shortstrings store length...
« Last Edit: June 13, 2017, 05:01:15 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

rvk

  • Hero Member
  • *****
  • Posts: 7018
Re: Rewrite(xxx) causes "Access denied"
« Reply #16 on: June 13, 2017, 05:01:20 pm »
If you use a record for file IO it should ALWAYS be a packed record.
It doesn't have to. But you're correct... It better to do so :D

When not switching to another language (or even version of the same language) the alignment will stay the same and you won't have any trouble with a normal record. But it's asking for trouble in the future.

(forcing a certain alignment is also an option but packed seems better)
« Last Edit: June 13, 2017, 05:03:07 pm by rvk »

Thaddy

  • Hero Member
  • *****
  • Posts: 19166
  • Glad to be alive.
Re: Rewrite(xxx) causes "Access denied"
« Reply #17 on: June 13, 2017, 05:26:30 pm »
When not switching to another language (or even version of the same language) the alignment will stay the same
This is not even always the case with FPC in the same compiler mode. Alignment can be configured away deep inside a fpc.cfg which can easily lead to mistakes.
So the correct answer is to always  pack the record for your own safety as well. (Without being pedantic, it is really the only way ever since TP introduced alignment at some point. I believe it was TP3.)

Ok: once the same software is compiled in the same alignment mode for read and write, you are correct. But it is SOOOO bad not to pack that I consider it an error. You will also loose diskspace, of course, small matter but it counts... >:D >:(
« Last Edit: June 13, 2017, 05:31:43 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

avra

  • Hero Member
  • *****
  • Posts: 2586
    • Additional info
Re: Rewrite(xxx) causes "Access denied"
« Reply #18 on: June 13, 2017, 05:36:39 pm »
Try to close file in final part of try..finally..end
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 19166
  • Glad to be alive.
Re: Rewrite(xxx) causes "Access denied"
« Reply #19 on: June 13, 2017, 06:09:30 pm »
Try to close file in final part of try..finally..end
That doesn't work on all platforms. It is simply the sharing mode that needs to be taken care of. Or a -sometimes long - time out.
objects are fine constructs. You can even initialize them with constructors.

RuudBaltissen

  • New Member
  • *
  • Posts: 14
    • Ruud's Homepage
Re: Rewrite(xxx) causes "Access denied"
« Reply #20 on: June 14, 2017, 07:27:56 am »
Can you provide the structure of aDatabase array and record?

const
  coMaxDatabase      =     9;


type
  tStr32  = string[32];

  tDatabase = record
   _wDatabase       : word;
   _sDbName         : tStr32;
  end;


var
  aDatabase        : array[0..coMaxDatabase] of tDatabase;

At this moment it means I only have room for 9 databases. But as written before, it already goes wrong with two entries.

Quote
Otherwise you need to strip down your program to just the reading and writing and see if your problem persists. If it does you can place your code here (which was already suggested).

I'm working on it....
« Last Edit: June 14, 2017, 08:54:25 am by RuudBaltissen »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Rewrite(xxx) causes "Access denied"
« Reply #21 on: June 14, 2017, 08:34:33 am »
Code: [Select]
const
  coMaxDatabase      =     9;
var
  aDatabase        : array[0..coMaxDatabase] of tDatabase;

At this moment it means I only have room for 9 databases.

That gives you space for 10 databases.

RuudBaltissen

  • New Member
  • *
  • Posts: 14
    • Ruud's Homepage
Re: Rewrite(xxx) causes "Access denied"
« Reply #22 on: June 14, 2017, 08:53:21 am »
Otherwise you need to strip down your program to just the reading and writing and see if your problem persists. If it does you can place your code here (which was already suggested).
The attached ZIP should contain everything except the generated EXE.

Start the program and replace <enter a name> by, for example, "Temp". Push TAB twice and change "Temp" in, for example, "Temp2". And do it again for "Temp3". Then exit the program. No error should have occured.
Now restart the program. List cbDatabase and you should see the values you entered previously. Now you can do two things: enter a new value or just close the program. In both cases you should receive an "Access denied" error.

Regarding the code: the functions "exists" and "shortS" date back from TP5.5, if not older. I'm so used to them that I never bothered to look if there is an equivalent in FreePascal or Lazarus.

And everyone thank you very much for every help/info so far and the future!

rvk

  • Hero Member
  • *****
  • Posts: 7018
Re: Rewrite(xxx) causes "Access denied"
« Reply #23 on: June 14, 2017, 08:59:27 am »
Bingo. I didn't run your code yet (I'm on a mobile phone) but already found your problem.

Your function exist() does a reset on the file (which opens it) but doesn't close it. So fix that or use the FPC version fileexists() from sysutils.
« Last Edit: June 14, 2017, 09:01:39 am by rvk »

RuudBaltissen

  • New Member
  • *
  • Posts: 14
    • Ruud's Homepage
Re: Rewrite(xxx) causes "Access denied"
« Reply #24 on: June 14, 2017, 09:09:53 am »
That gives you space for 10 databases.
Should be enough for the moment.

Some background:
I'm a Commodore fan: Commodore 64, PET-2001, CBM 8032, VIC-20 etc. etc. At this moment I am creating images of all floppy disks I have. Other people are doing that as well. So we have tons of material. But AFAIK there is no real database what already has been imaged. And that's what I'm working on, a database where I can see what disks I have and what programs they contain. And more important: I have a mean to filter all double files so I can reduce the collection to a minimum.
At the end I/we hope to present the world with a complete collection of all available software for the various Commodore computers.
« Last Edit: June 14, 2017, 09:28:21 am by RuudBaltissen »

RuudBaltissen

  • New Member
  • *
  • Posts: 14
    • Ruud's Homepage
Re: Rewrite(xxx) causes "Access denied"
« Reply #25 on: June 14, 2017, 09:20:05 am »
Bingo. I didn't run your code yet (I'm on a mobile phone) but already found your problem.

Your function exist() does a reset on the file (which opens it) but doesn't close it. So fix that or use the FPC version fileexists() from sysutils.
WOW! It is indeed the troubleshooter, things work fine now. I really wonder why I had no problems with it before. A lesson for me: when having unexplainable troubles again, check all this old stuff first.

Thank you very much !!!

Kind regards, Ruud Baltissen

rvk

  • Hero Member
  • *****
  • Posts: 7018
Re: Rewrite(xxx) causes "Access denied"
« Reply #26 on: June 14, 2017, 10:40:29 am »
I really wonder why I had no problems with it before.
Well, if this piece of snippet is from TP 5.5 it's from 1989. Back in the MS-DOS days you had a utility SHARE.EXE which would handle file locks. The loading of SHARE.EXE was voluntary so if you didn't use it, you probably could open the file multiple times, regardless of FileMode, and it would all still work.

But when converting old TP code it's always best to see if there are alternatives for such functions.

So it's best that you use FileExists() instead of your Exist(). I also saw your ShortS(). You could use Trim() in SysUtils for that. (so just remove those functions from your program and see where the compiler gives you errors and use the new functions there)

One final note... although you check for the existence of the file before you open (or create) it... it's always best to handle errors. Even though in your case these errors don't present themselves doesn't mean they never will on other systems. So use {$I-}, {$I+} and IOResult around all rewrite and reset calls and handle the error accordingly.

RuudBaltissen

  • New Member
  • *
  • Posts: 14
    • Ruud's Homepage
Re: Rewrite(xxx) causes "Access denied"
« Reply #27 on: June 14, 2017, 12:24:14 pm »
But when converting old TP code it's always best to see if there are alternatives for such functions.
The reasons I kept on using this old code is me being a very lazy person and not having much free time. But this was a good lesson for me.

Quote
So it's best that you use FileExists() instead of your Exist(). I also saw your ShortS(). You could use Trim() in SysUtils for that.
Thank you for these pointers.

Quote
One final note... although you check for the existence of the file before you open (or create) it... it's always best to handle errors. Even though in your case these errors don't present themselves doesn't mean they never will on other systems. So use {$I-}, {$I+} and IOResult around all rewrite and reset calls and handle the error accordingly.
OK, I'll do that from now in.

Many thanks again!

 

TinyPortal © 2005-2018