Board index » delphi » Problems using seek and blockwrite

Problems using seek and blockwrite

Quote
Jim Wilson wrote in message <3898eedd.2914...@netnews.worldnet.att.net>...
>Are seek and blockwrite "incompatible"? I have the following function
>which is supposed to copy a block of data to a new file, or append it
>to an existing one depending upon the state of the AppendFile
>variable. But it doesn't append. It copies fine, but the append works
>exactly like copy does; e.g. the file is the same as opposed to being
>appended to. Is this function wrong some how?  BTW; and before anyone
>asks ... yes, AppendFile is TRUE when this function is accessed.

>Function WriteFile : byte;

>begin

>  {$I-}
>    if AppendFile then seek (TargetFile,filesize (TargetFile));
>    blockwrite (TargetFile,Buffer,BytesRead,BytesWritten);
>  {$I+}

>  WriteFile := ioresult;

>end;

>Regards,
>Jim Wilson
>(Cheap spam protection in place: remove the 'SpamThis'
>from my email ID to reply to me personally)

Jim,

There is nothing wrong with the code fragment you submitted.
It appended data very nicely to my test file once I expanded
your code to a full program. I think the problem lies elsewhere.
Did you remember to use reset(TargetFile,1) when opening
your output file?

KlausL

 

Re:Problems using seek and blockwrite


Quote
Jim Wilson wrote:

> Are seek and blockwrite "incompatible"? I have the following function
> which is supposed to copy a block of data to a new file, or append it
> to an existing one depending upon the state of the AppendFile
> variable. But it doesn't append. It copies fine, but the append works
> exactly like copy does; e.g. the file is the same as opposed to being
> appended to. Is this function wrong some how?  BTW; and before anyone
> asks ... yes, AppendFile is TRUE when this function is accessed.

> Function WriteFile : byte;

> begin

>   {$I-}
>     if AppendFile then seek (TargetFile,filesize (TargetFile));
>     blockwrite (TargetFile,Buffer,BytesRead,BytesWritten);
>   {$I+}

>   WriteFile := ioresult;

> end;

Seek and BlockWrite are fully compatible.

Besides the poor programming you made a mistake: BlockWrite does
not deliver an IOresult if you added the "BytesWritten" parameter.

The poor programming: You did not check the IOresult following
the SEEK command.

I assume that your error is not shown here. Did you
  Reset(TargetFile,1);
???

:-)
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Re:Problems using seek and blockwrite


Quote
Jim Wilson wrote in message <3898eedd.2914...@netnews.worldnet.att.net>...
>Are seek and blockwrite "incompatible"?

The short answer to this is, "no". I use them together all the time. Given
that existence theorem you can look for a bug elsewhere. The way I normally
use them is with f:FILE; in other words with untyped files and a buffer
array of either char or byte which means you use Rewrite(f, 1) and Reset(f,
1).

 I have the following function

Quote
>which is supposed to copy a block of data to a new file, or append it
>to an existing one depending upon the state of the AppendFile
>variable. But it doesn't append. It copies fine, but the append works
>exactly like copy does; e.g. the file is the same as opposed to being
>appended to. Is this function wrong some how?  BTW; and before anyone
>asks ... yes, AppendFile is TRUE when this function is accessed.

>Function WriteFile : byte;

>begin

>  {$I-}
>    if AppendFile then seek (TargetFile,filesize (TargetFile));
>    blockwrite (TargetFile,Buffer,BytesRead,BytesWritten);
>  {$I+}

>  WriteFile := ioresult;

>end;

>Regards,
>Jim Wilson
>(Cheap spam protection in place: remove the 'SpamThis'
>from my email ID to reply to me personally)

Re:Problems using seek and blockwrite


In article <3898eedd.2914...@netnews.worldnet.att.net>,

Quote
Jim Wilson <SpamThis!jawil...@ix.netcom.com> wrote:
>Are seek and blockwrite "incompatible"? I have the following function
>which is supposed to copy a block of data to a new file, or append it
>to an existing one depending upon the state of the AppendFile
>variable. But it doesn't append. It copies fine, but the append works
>exactly like copy does; e.g. the file is the same as opposed to being
>appended to. Is this function wrong some how?  BTW; and before anyone
>asks ... yes, AppendFile is TRUE when this function is accessed.

>Function WriteFile : byte;

>begin

>  {$I-}
>    if AppendFile then seek (TargetFile,filesize (TargetFile));
>    blockwrite (TargetFile,Buffer,BytesRead,BytesWritten);
>  {$I+}

>  WriteFile := ioresult;

This is not related to the problem, but if you use the BytesWritten
parameter, then you should check it. Alternatively you could drop it and
let the IOResult handle it,

Osmo

Re:Problems using seek and blockwrite


On Thu, 3 Feb 2000 10:45:03 -0600, "Clif Penn" <clifp...@airmail.net>
wrote:

Quote
>Jim Wilson wrote in message <3898eedd.2914...@netnews.worldnet.att.net>...
>>Are seek and blockwrite "incompatible"?
>The short answer to this is, "no". I use them together all the time. Given
>that existence theorem you can look for a bug elsewhere. The way I normally
>use them is with f:FILE; in other words with untyped files and a buffer
>array of either char or byte which means you use Rewrite(f, 1) and Reset(f,
>1).

I must have missed something else, I suppose. I'm going to go back and
check it now. Thanks.

Regards,
Jim Wilson
(Cheap spam protection in place: remove the 'SpamThis'
from my email ID to reply to me personally)

Re:Problems using seek and blockwrite


On 3 Feb 2000 21:42:19 +0200, ronka...@cc.helsinki.fi (Osmo Ronkanen)
wrote:

Quote
>This is not related to the problem, but if you use the BytesWritten
>parameter, then you should check it. Alternatively you could drop it and
>let the IOResult handle it,

After looking it over again I can see where the error checking could
be made better, as well as a few other things. Thanks for the tip
though.

Regards,
Jim Wilson
(Cheap spam protection in place: remove the 'SpamThis'
from my email ID to reply to me personally)

Re:Problems using seek and blockwrite


On Thu, 03 Feb 2000 17:10:07 +0100, "Ing. Franz Glaser"

Quote
<meg-gla...@eunet.at> wrote:
>Seek and BlockWrite are fully compatible.

>Besides the poor programming you made a mistake: BlockWrite does
>not deliver an IOresult if you added the "BytesWritten" parameter.

>The poor programming: You did not check the IOresult following
>the SEEK command.

Solutions or suggestions would have sufficed. Judgmental comments
and/or sarcasm is not welcome, as well as unnecessary, so keep those
to yourself.

Regards,
Jim Wilson
(Cheap spam protection in place: remove the 'SpamThis'
from my email ID to reply to me personally)

Re:Problems using seek and blockwrite


Quote
Jim Wilson wrote:

> On Thu, 3 Feb 2000 10:45:03 -0600, "Clif Penn" <clifp...@airmail.net>
> wrote:
> >The short answer to this is, "no". I use them together all the time. Given
> >that existence theorem you can look for a bug elsewhere. The way I normally
> >use them is with f:FILE; in other words with untyped files and a buffer
> >array of either char or byte which means you use Rewrite(f, 1) and Reset(f,
> >1).

> I must have missed something else, I suppose. I'm going to go back and
> check it now. Thanks.

Of course, it must be in the Reset(F,nnn) instruction. But you
have been so offended from my posting that you simply did not
read my suggestions. ;-)
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Re:Problems using seek and blockwrite


Quote
Jim Wilson wrote:

> On Thu, 03 Feb 2000 17:10:07 +0100, "Ing. Franz Glaser"
> <meg-gla...@eunet.at> wrote:

> >Seek and BlockWrite are fully compatible.

> >Besides the poor programming you made a mistake: BlockWrite does
> >not deliver an IOresult if you added the "BytesWritten" parameter.

> >The poor programming: You did not check the IOresult following
> >the SEEK command.

> Solutions or suggestions would have sufficed. Judgmental comments
> and/or sarcasm is not welcome, as well as unnecessary, so keep those
> to yourself.

Oh, I am sorry, Jim. I misinterpreted your question as if you
wanted to learn something. But of course, I should have considered
that you are a great programmer, only with files you have some
leakages...

On the other hand, if I see poor source text anywhere on my screen,
I critisize, regardless of the authors sensitivities and youngster
behaviour. This is because I am a fanatic defender of Pascal style.

Ok?
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Re:Problems using seek and blockwrite


In article <3899A85F.71CFA...@eunet.at>,
Ing. Franz Glaser <meg-gla...@eunet.at> wrote:

Quote
>Jim Wilson wrote:

>> Are seek and blockwrite "incompatible"? I have the following function
>> which is supposed to copy a block of data to a new file, or append it
>> to an existing one depending upon the state of the AppendFile
>> variable. But it doesn't append. It copies fine, but the append works
>> exactly like copy does; e.g. the file is the same as opposed to being
>> appended to. Is this function wrong some how?  BTW; and before anyone
>> asks ... yes, AppendFile is TRUE when this function is accessed.

>> Function WriteFile : byte;

>> begin

>>   {$I-}
>>     if AppendFile then seek (TargetFile,filesize (TargetFile));
>>     blockwrite (TargetFile,Buffer,BytesRead,BytesWritten);
>>   {$I+}

>>   WriteFile := ioresult;

>> end;

>Seek and BlockWrite are fully compatible.

>Besides the poor programming you made a mistake: BlockWrite does
>not deliver an IOresult if you added the "BytesWritten" parameter.

>The poor programming: You did not check the IOresult following
>the SEEK command.

And why should it be checked?

Osmo

Re:Problems using seek and blockwrite


Quote
Osmo Ronkanen wrote:

> In article <3899A85F.71CFA...@eunet.at>,
> Ing. Franz Glaser <meg-gla...@eunet.at> wrote:
> >Jim Wilson wrote:

> >> Are seek and blockwrite "incompatible"? I have the following function
> >> which is supposed to copy a block of data to a new file, or append it
> >> to an existing one depending upon the state of the AppendFile
> >> variable. But it doesn't append. It copies fine, but the append works
> >> exactly like copy does; e.g. the file is the same as opposed to being
> >> appended to. Is this function wrong some how?  BTW; and before anyone
> >> asks ... yes, AppendFile is TRUE when this function is accessed.

> >> Function WriteFile : byte;

> >> begin

> >>   {$I-}
> >>     if AppendFile then seek (TargetFile,filesize (TargetFile));
> >>     blockwrite (TargetFile,Buffer,BytesRead,BytesWritten);
> >>   {$I+}

> >>   WriteFile := ioresult;

> >> end;

> >Seek and BlockWrite are fully compatible.

> >Besides the poor programming you made a mistake: BlockWrite does
> >not deliver an IOresult if you added the "BytesWritten" parameter.

> >The poor programming: You did not check the IOresult following
> >the SEEK command.

> And why should it be checked?

Osmo,

this is a very good question. It is true that in normal
operations the seek(F,filesize(F)) could not make troubles.

But I have multitasking and multiuser fileops in mind, where
it can make troubles indeed. So I do a correct IOresult check
on EVERY I/O command. In a very similar program example I
had expensive problems with a customer installation, when
the disk was actually a diskette, which could be removed
meanwhile.

The real problem is that if Seek failed, BlockIO in the example
would not be executed. But an experienced programmer would not
check the IOresult following BlockRW.

Btw.: I must admit that my sentence concerning the "EVERY" above
is more a "religious" statement that the truth :-) But in most
cases the punishment follows, and it is often expensive, costing
many kilometers for service at the customers site.

Regards,
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Re:Problems using seek and blockwrite


In article <389D4D81.1B337...@eunet.at>,
Ing. Franz Glaser <meg-gla...@eunet.at> wrote:

Quote
>Osmo Ronkanen wrote:

>> And why should it be checked?

>Osmo,

>this is a very good question. It is true that in normal
>operations the seek(F,filesize(F)) could not make troubles.

>But I have multitasking and multiuser fileops in mind, where
>it can make troubles indeed. So I do a correct IOresult check
>on EVERY I/O command. In a very similar program example I
>had expensive problems with a customer installation, when
>the disk was actually a diskette, which could be removed
>meanwhile.

>The real problem is that if Seek failed, BlockIO in the example
>would not be executed. But an experienced programmer would not
>check the IOresult following BlockRW.

Why not? IMO it is good to check the error there. Of course checking the
count is enough. Blockread/write can fail even if there is the fourth
parameter.It might be simpler to leave the last parameter out and
use IOresult.

Quote

>Btw.: I must admit that my sentence concerning the "EVERY" above
>is more a "religious" statement that the truth :-) But in most
>cases the punishment follows, and it is often expensive, costing
>many kilometers for service at the customers site.

I say i most cases it is simpler to do the stuff and the check. The
more checks you put the more probable it is that you will miss the
correct place. Also extra checks can make the code complicated unless
one uses break, exit or goto.

Osmo

- Show quoted text -

Quote
>Regards,
>--
>Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
>Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
>http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
>http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Re:Problems using seek and blockwrite


In article <87k0qn$cv...@kruuna.helsinki.fi>,

Quote
Osmo Ronkanen <ronka...@cc.helsinki.fi> wrote:
>In article <389D4D81.1B337...@eunet.at>,
>Ing. Franz Glaser <meg-gla...@eunet.at> wrote:

>Why not? IMO it is good to check the error there. Of course checking the
>count is enough.

Now I said something really stupid. Of course checking the mere number
of bytes written is not enough as that does not clear the error flag.
One must check ioresult after blockwrite.

Osmo

Re:Problems using seek and blockwrite


In article <389bb640.11113...@netnews.worldnet.att.net>,
SpamThis!jawil...@ix.netcom.com says...

Quote
> On Thu, 03 Feb 2000 17:10:07 +0100, "Ing. Franz Glaser"
> <meg-gla...@eunet.at> wrote:

> >Seek and BlockWrite are fully compatible.

> >Besides the poor programming you made a mistake: BlockWrite does
> >not deliver an IOresult if you added the "BytesWritten" parameter.

> >The poor programming: You did not check the IOresult following
> >the SEEK command.

> Solutions or suggestions would have sufficed. Judgmental comments
> and/or sarcasm is not welcome, as well as unnecessary, so keep those
> to yourself.

   You made a major misjudgment in your (mis)reading of Franz's reply to
you.  First, there was no "sarcasm" in his message, and it _was_ entirely
appropriate to inform your that what you did, while syntactically
correct, was a poor way to do it - by failing to use (all) the
appropriate "tools" available in the language to accomplish your task.
   Second, you haven't been around here very long, so you don't know that
Franz Glaser is one to the true "giants" here.  A regular and frequent
contributor here, he knows his stuff, and always presents his arguments
thoughtfully and rationally.  Unlike many, Franz offers good and proper
advice, and you made a serious mistake in misinterpreting his reply.  
Then you compounded in by flaming one of the "Good Guys"...

Re:Problems using seek and blockwrite


In article <MPG.13073ef2bb664212989...@news.primenet.com>,

Quote
Mike Copeland <mrc...@primenet.com> wrote:

>   You made a major misjudgment in your (mis)reading of Franz's reply to
>you.  First, there was no "sarcasm" in his message, and it _was_ entirely
>appropriate to inform your that what you did, while syntactically
>correct, was a poor way to do it - by failing to use (all) the
>appropriate "tools" available in the language to accomplish your task.

The code was as follows:
 {$I-}
   if AppendFile then seek (TargetFile,filesize (TargetFile));
   blockwrite (TargetFile,Buffer,BytesRead,BytesWritten);
 {$I+}

 WriteFile := ioresult;

It is perfectly OK with the exception that the BytesWritten is not
checked, so I'd use:

 {$I-}
   if AppendFile then seek (TargetFile,filesize (TargetFile));
   blockwrite (TargetFile,Buffer,BytesRead);
 {$I+}

 WriteFile := ioresult;

Now it is OK. There is no need to check the ioresult after the seek. If
the seek fails, then the blockwrite will not be executed.

Osmo

Go to page: [1] [2]

Other Threads