BLOCKREAD&BLOCKWRITE? 
Author Message
 BLOCKREAD&BLOCKWRITE?

HI FOLKS!
Can anyone explain me how to use blockread and blockwrite in order to write
and read from a file that isn't a text file?

   Best Regards,
               Alex



Mon, 08 Sep 2003 04:26:43 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:

>Can anyone explain me how to use blockread and blockwrite in order to write
>and read from a file that isn't a text file?

Briefly, if the file is in records

type ThisRecord = record ...

const BuffSize = some number, to make the buff array a few KB

var buff : array[ 1 .. BuffSize] of record;

f : file;

...

repeat
  BlockRead( f, buff, BuffSize, recs);

now Buff contains recs records

until recs <> BuffSize;



Mon, 08 Sep 2003 04:45:28 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:

>...

I forgot

assign( f, filename);
reset( f, sizeof( ThisRecord));

...

close( f);



Mon, 08 Sep 2003 08:25:58 GMT  
 BLOCKREAD&BLOCKWRITE?


Quote:

> >Can anyone explain me how to use blockread and blockwrite in order
to write
> >and read from a file that isn't a text file?

> Briefly, if the file is in records

> type ThisRecord = record ...

> const BuffSize = some number, to make the buff array a few KB

> var buff : array[ 1 .. BuffSize] of record;

I'd normally use char or byte instead of a record type. If the file
has a record structure then the file could be declared as a file of
record, and be handled more easily, so I would assume the length of
the file is arbitrary. Therefore a buffer element size of 1 byte is
appropriate.

Quote:
> f : file;

> ...

> repeat

assign(f, FileName);
reset (f, 1); { or sizeof(Record) if you're going that way }
Quote:
>   BlockRead( f, buff, BuffSize, recs);

> now Buff contains recs records

> until recs <> BuffSize;



Mon, 08 Sep 2003 22:57:11 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:

>I'd normally use char or byte instead of a record type. If the file
>has a record structure then the file could be declared as a file of
>record,

I don't think you can use BlockRead and BlockWrite on a typed
file.  And with the buffer array being of the record type, no
typecasting is needed.


Tue, 09 Sep 2003 02:39:35 GMT  
 BLOCKREAD&BLOCKWRITE?



Quote:

>>I'd normally use char or byte instead of a record type. If the file
>>has a record structure then the file could be declared as a file of
>>record,

>I don't think you can use BlockRead and BlockWrite on a typed
>file.  And with the buffer array being of the record type, no
>typecasting is needed.

There is no such thing as a typed file.

One uses BlockRead(F, ... where F is an untyped file variable,
certainly; but any file can be opened in that manner.  Even a file that
was created as, and is suitable for being read as, a file of text.

A file of right-justified text (designed fixed-width) could be
effectively read with BlockRead as untyped, or as a file of string [n],
or as text.

  This  is
  a little
  example.

--

 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.



Tue, 09 Sep 2003 07:07:12 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:
>There is no such thing as a typed file.

Hmmm.  The BP help for "file" says: "A file type consists of a
linear sequence of components of the component _type_ which can
be any type except a file type. ...  If the word _of_ and the
component type are omitted, the type denotes an untyped file."


Tue, 09 Sep 2003 09:25:27 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:
>There is no such thing as a typed file.

BP 7 Language Guide, page 286, under "File types", it has "Typed
files and untyped files occupy 128 bytes ...".


Tue, 09 Sep 2003 09:35:58 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:


> >I'd normally use char or byte instead of a record type. If the file
> >has a record structure then the file could be declared as a file of
> >record,

> I don't think you can use BlockRead and BlockWrite on a typed
> file.  And with the buffer array being of the record type, no
> typecasting is needed.

BlockRead/Write use the untyped 'File'


Tue, 09 Sep 2003 11:36:39 GMT  
 BLOCKREAD&BLOCKWRITE?


Quote:

> >I'd normally use char or byte instead of a record type. If the file
> >has a record structure then the file could be declared as a file of
> >record,

> I don't think you can use BlockRead and BlockWrite on a typed
> file.  And with the buffer array being of the record type, no
> typecasting is needed.

Whatever the program requires... If you are reading records, if you
created the files yourself by dumping records, then of course you can
use an array of records for the buffer. It's just that
BlockRead/BlockWrite are [also] used files where you don't know that
the contents are going to be binary compatible with any record type.
And in that case, since you don't know that you will get an integral
number of records, you can use a "record size" of 1 byte.

A caveat: In MS-DOS/Windows, a file of some record type contains just
binary images of the records, and can be read using untyped files as
you describe. And I would expect similar behaviour in other modern
OSes, because these can accurately report the size of a file. But this
is not true for all versions of TurboPascal in all OSes.

And perhaps I should mention for the original poster, that while
  reset(f, 1);
specifies a "record size" of 1 byte, where f is the untyped file,
  reset(f);
is also correct and uses a "record size" of 128 bytes, for historical
reasons. You would almost never want this, but the compiler cannot
complain about it! This can be an awkward bug to find, if you don't
know about it.

FP



Tue, 09 Sep 2003 18:15:52 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:

>A caveat: In MS-DOS/Windows, a file of some record type contains just
>binary images of the records, and can be read using untyped files as
>you describe. And I would expect similar behaviour in other modern
>OSes, because these can accurately report the size of a file. But this
>is not true for all versions of TurboPascal in all OSes.

How true. Fore example VAX/VMS has several different file types. It is a
pain to write for a program that handles binary files on those systems
as a generic binary file (like ZIP, GIF etc.) could use several
different formats depending on the software used to create or upload the
file.

The file as a stream of bytes like in DOS or UNIX is much nicer.

Osmo



Tue, 09 Sep 2003 20:32:55 GMT  
 BLOCKREAD&BLOCKWRITE?


Quote:


> >A caveat: In MS-DOS/Windows, a file of some record type contains
just
> >binary images of the records, and can be read using untyped files
as
> >you describe. And I would expect similar behaviour in other modern
> >OSes, because these can accurately report the size of a file. But
this
> >is not true for all versions of TurboPascal in all OSes.

> How true. Fore example VAX/VMS has several different file types. It
is a
> pain to write for a program that handles binary files on those
systems
> as a generic binary file (like ZIP, GIF etc.) could use several
> different formats depending on the software used to create or upload
the
> file.

Wow- a VAX/VMS version of TurboPascal! :)

I was thinking of the more mundane CP/M TP3 - the records were
preceded by a couple of bytes indicating the exact file size, because
the OS couldn't give it more exact than 128 bytes; the DOS versions of
the same compiler didn't have or need this header.

FP



Tue, 09 Sep 2003 21:12:11 GMT  
 BLOCKREAD&BLOCKWRITE?

Quote:

> It's just that
>BlockRead/BlockWrite are [also] used files where you don't know that
>the contents are going to be binary compatible with any record type.

That's right, but normally you know what's in the file you're
reading, and he asked about blockwrite also, so he knows what he
is writing.


Tue, 09 Sep 2003 23:17:44 GMT  
 BLOCKREAD&BLOCKWRITE?


Quote:

> > It's just that
> >BlockRead/BlockWrite are [also] used files where you don't know
that
> >the contents are going to be binary compatible with any record
type.

> That's right, but normally you know what's in the file you're
> reading, and he asked about blockwrite also, so he knows what he
> is writing.

Ok, fair enough.

(but it still isn't necessarily in identically-sized record-shaped
units all the way through, that's all I was saying)

Ah no, it's Friday evening, where's the online help for BLOCKDRINK?

FP



Wed, 10 Sep 2003 01:39:52 GMT  
 BLOCKREAD&BLOCKWRITE?



Quote:

>>There is no such thing as a typed file.

>Hmmm.  The BP help for "file" says: "A file type consists of a
>linear sequence of components of the component _type_ which can
>be any type except a file type. ...  If the word _of_ and the
>component type are omitted, the type denotes an untyped file."

It is wrong.  The type denotes a file which has been opened as
"untyped".  It is a property of the access and not of the file itself.

Just try it: create a file, and open and close it as untyped, typed,
text.  And look into the DOS directory entry, which is the only place
where DOS could store file type information; there is nothing.

Consider, for example, TSFAQP #62, where BlockRead and BlockWrite are
used, with var ... f1, f2 : file;.  There is no mention of the code
being restricted in the sorts of file that it can handle.  Likewise
TAFAQP #90.

Of course, it does not make a lot of sense to open an.exe as a text
file; but it can be done.

--

 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.



Wed, 10 Sep 2003 00:18:45 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Blockread/Blockwrite question.

2. Blockread/Blockwrite question

3. Urgent help needed with GFx mode and BlockRead/BlockWrite

4. blockread/blockwrite

5. Blockread / Blockwrite

6. How do I use BLOCKREAD/BLOCKWRITE

7. Problems using seek and blockwrite

8. Blockwrite

9. blockwrite anomaly?

10. BlockWrite

11. BlockWrite problem.

12. Blockwrite constant

 

 
Powered by phpBB® Forum Software