I've got myself into a bit of trouble again, I fear... While reading a
file, I get the message 'Project raised exception class EInOutError with
message File Access Denied'. When I dig a little deeper, I get the error
I/O error 103, which according to my guide means that the file could not
be opened.
Here is the code I've written:
--- function ---
function readFile(const a : string) : boolean;
var
af : File;
aBuff : array [1..1024] of Byte;
aLen : LongInt;
idx : LongInt;
read : boolean;
begin
read := true;
try
try
Assign(af, a);
Reset(af, 1);
while (not Eof(af)) do
begin
BlockRead(af, aBuff, SizeOf(aBuff), aLen);
end;
except
on EInOutError do
begin
read := false;
end;
end;
finally
begin
Close(af);
end;
end;
readFile := read;
end;
---
The call to this function is simple: I've added a simple button to the
form in order to test the function, and behind the button is the
following line of code.
---
success := readFile('f:\temp\movies\movie.wmv');
---
The file exists in that particular location, and it is not in use. I can
copy the file just fine in explorer or other shells, so a read error
should not occur.
Can anybody please explain to me what I'm missing here? Also, comments on
the code or improvements are, as always, more than welcome!
Thanks in advance,
Ikke
> Here is the code I've written:
Do you allways get an error?
I've tested the code on a file and it seems
to work fine (no error, returns true).
I found the error!
Yes, the error always occurs for a given file. But I was testing several
files within a directory, and I've now found out that all of those files
are read-only.
So after a bit of browsing I found that I needed to add:
FileMode := fmOpenRead;
prior to calling reset on the file.
Now it works like a charm (or as a filereader)...
Thanks for the reply!
Ikke
> I've got myself into a bit of trouble again, I fear... While reading a
> file, I get the message 'Project raised exception class EInOutError with
> message File Access Denied'. When I dig a little deeper, I get the error
> I/O error 103, which according to my guide means that the file could not
> be opened.
Sorry, no idea what could cause that error, in your case.
> while (not Eof(af)) do
> begin
> BlockRead(af, aBuff, SizeOf(aBuff), aLen);
> end;
It's a good idea to tally the bytes transferred. Then you can tell on an
exception, whether reading has started at all.
DoDi
> Ikke wrote:
>
>> I've got myself into a bit of trouble again, I fear... While reading
>> a file, I get the message 'Project raised exception class EInOutError
>> with message File Access Denied'. When I dig a little deeper, I get
>> the error I/O error 103, which according to my guide means that the
>> file could not be opened.
>
> Sorry, no idea what could cause that error, in your case.
As I've already replied to Andreas, the file was read-only. As were all
the other files I've tested with in the same directory :(
Adding "FileMode := fmOpenRead;" prior to the Reset fixed it.
>> while (not Eof(af)) do
>> begin
>> BlockRead(af, aBuff, SizeOf(aBuff), aLen);
>> end;
>
> It's a good idea to tally the bytes transferred. Then you can tell on
> an exception, whether reading has started at all.
Thanks for the advice - a good tip to remember!
Ikke
Personally I'd use file streams for file IO, but that may be because
I've never got into Assign / Reset / Read (or write), and streams
(memory, file, string) are more convenient IMO.
Similar stream code would be ...
var
FS : TFileStream;
Buff : array[0..1023] of byte; // conventionally Delphi arrays (and
indexes) start from 0
begin
FS := TFileStream.Create(A, fmOpenRead);
// fmCreate , fmOpenReadWrite, etc, can be specified as the
file mode, and share modes
// can be or-ed with the filemode
try
FS.Seek(0, soFromBeginning); // move to first byte,
unnecessary on opening but cautionary
ALen := FS.Read(Buff, SizeOf(Buff));
finally
FS.Free;
end;
end;
There is a Streams Help file on the FAQ site which would give you more
information.
Alan Lloyd
InOutRes := 0;
do that before you start a group of Pascal IO code..
You can also check this for a value with out it being
cleared unlike the IoResult variable.
WHen you're done debugging your app and ready for release,
you should do IO checking your self instead of it generating
an error message. It does not look to professional that way :)
{$I-} is the compiler switch or, you can set that in your
project options.
--
"I'm never wrong, once i thought i was, but was mistaken"
Real Programmers Do things like this.
http://webpages.charter.net/jamie_5