Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EInOutError while reading a file

1,065 views
Skip to first unread message

Ikke

unread,
May 12, 2007, 10:50:33 AM5/12/07
to
Hi everybody,

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

Andreas Koch

unread,
May 12, 2007, 11:37:04 AM5/12/07
to
Ikke wrote:

> 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).


Ikke

unread,
May 12, 2007, 11:40:40 AM5/12/07
to
Andreas Koch <nos...@kochandreas.com> wrote in news:f24mqc$2ga$00$1
@news.t-online.com:

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

Hans-Peter Diettrich

unread,
May 12, 2007, 2:55:55 PM5/12/07
to
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.


> 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

unread,
May 13, 2007, 8:23:43 AM5/13/07
to
Hans-Peter Diettrich <DrDiet...@aol.com> wrote in
news:5amhchF...@mid.individual.net:

> 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

alang...@aol.com

unread,
May 13, 2007, 10:29:36 AM5/13/07
to
If you're running in the {$I+} compiler directive setting (IO checking
On - default) then any lingering error at the start of an IO operation
can give an error on a normal operation. You should therefore call
IOResult to clear any IO errors befare reading from the file.

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

Jamie

unread,
May 13, 2007, 11:44:46 AM5/13/07
to
As a side note, you should clear the background io status from the
previous operation to make sure things go as should on the next process.
Some times, people leave the status hanging from the last operation
that may had failed depending on how they coded it.

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

0 new messages