"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?
aztecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
hi,
try cjhanging the filemode to r/o

AssignFile(F, FileName);
FileMode := 0;  ( Set file access to read only }
  Reset(F);
 ..
  ...
 ..
  CloseFile(F);

Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
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 :-)
0
aztecAuthor Commented:
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
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

intheCommented:
will the file load into a memo or stringlist at all ?
how are you trying to open it?
0
kifahCommented:
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
0
intheCommented:
jeeez...no comment ...
0
aztecAuthor Commented:
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
0
intheCommented:
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
0
ianBCommented:
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
0
aztecAuthor Commented:
Thanks Barry!

Cheers
   Shawn
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.