Link to home
Start Free TrialLog in
Avatar of aztec
aztec

asked on

"File Access Denied" error with read-only files

Hi ...
  Whenever my Delphi 3 app tries to open and read a file which is set to "Read-Only" mode, it gives a "File Access Denied" error. I'm opening and reading these files as simple text files. I only want to read from these files, not write to them...so why would I get an error trying to read from them? I know one solution is to change the file's properties to 'Archive', but is there a way programmatically to successfully read these 'read-only' files?

Thanks
   Shawn Halfpenny
   drumme59@sprint.ca

P.S: My 'automatic notification' thing is not working whenever you guys answer my questions...how can I fix this?
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of inthe
inthe

btw.
the 'automatic notification' problem try clicking on the blue hyperlink of your username (aztec) and edit your profile ,check your email address is set correctly.if it is correct send a mail(or ask a question) to customer services department and tell them and they will have a look.
cheers :-)
Avatar of aztec

ASKER

According to the help file, the Filemode command is not for text files - which is what I'm using. What do I do now?

Thanks
   Shawn
will the file load into a memo or stringlist at all ?
how are you trying to open it?
The simple way is to use the TStringlist. In fact the TString list has a lot of methodes which can help you to handle the string.

var
  MyFile: TStringList;
begin
  try
    MyFile := TStringList.Create;
    MyFile.LoadFromFile(FileName);
   
  finally
    MyFile.Free;
    MyFile := nil; {Make sure it's memory refrence is freed}
  end;
end;

as well see the other 2 way to read the text file

1-Option 1
var

  F: TextFile;
  S: string;
begin
  if OpenDialog1.Execute then        
  begin
    FileMode := 0;
    AssignFile(F, OpenDialog1.FileName);  
    Reset(F);
    Readln(F, S);                      
    Edit1.Text := S;                
    CloseFile(F);
  end;
end;

2- Option 2
You can use the TFileStream, which is good as well, but it is not so easy.

var
  FStream: TFileStream;
begin
  try
    FStream := TFileStream.Create(FileName, fmOpenRead);
    Read any part of the file.
  finally
    FStream.Free;
    FStream := nil;
  end;
end;

But still the first methode (TStringList), the best and simple way.

Best Regards
Kifah R. Najem
jeeez...no comment ...
Avatar of aztec

ASKER

Sorry guys...my mistake...yep, I'm an idiot. The problem I was having wasn't with text files after all. It was with the typed files I had - and as soon as I set filemode to 0 as "inthe" suggested, it worked fine. Can you please give "inthe" full marks for his answer.

Sorry!
   Shawn Halfpenny
glad you got it working
you have to reject the answer of kifah
(one of the options on the page)then i can place an answer for you to accept.
cheers
Hi,

If the proposed answer is not by the expert you want to receive the points, then choose the option below the question " 2.Reject xxx's
proposed answer and reopen the question to other experts."  


Then you can accept the comment you want, by clicking the "accept comment as answer" link.

Ian
community Support @ Experts Exchange
Avatar of aztec

ASKER

Thanks Barry!

Cheers
   Shawn