help need with BlockRead 
Author Message
 help need with BlockRead

I'm trying to write a small TP5.5 program to split a large
binary file (say, 30 MB) into 1.4 MB chunks.
My problem was that BlockRead() didn't read from the input
file (i.e. NumRead returned zero, see code below).

What did I do wrong?

Program split;
{Split a file into 1.4M chunks}
Const
  limit = 28;
  BufferSize = 51200;

Type
  Buffer = array [1..BufferSize] of byte;

var
  m : integer;
  NumRead, NumWritten : word;
  F1 : file;
  F2 : file;
  s : string;
  infile : string;
  outfile : string;
  n : longint;
  chunk : Buffer;

begin
  writeln('Split a file into ',(limit*BufferSize):0,' byte-chunks');
  writeln;
  write ('file name of source file : ');
  readln (infile);
  Assign (F1, infile);
  reset (F1);
  m := 1;
  n := 0;
  outfile := 'F01';
  Assign (F2, outfile);
  Rewrite(F2);
  while not eof(F1) do
  begin
    BlockRead (F1, chunk, Sizeof(chunk), NumRead );
    if (NumRead <= 0) then
      begin
        writeln ('NumRead = ', NumRead);
        Close (F1);
        Close (F2);
        exit;
      end;
    BlockWrite (F2, chunk, NumRead, NumWritten);
    n := n+1;
    if (n >= limit) then
     begin
       close(F2);
       writeln(outfile, ' created');
       m := m+1;
       if (m <= 9) then
         begin
           Str(m,s);
           outfile := 'F0'+ s;
         end
       else
         begin
           Str(m,s);
           outfile := 'F'+ s;
         end;
       Assign (F2, outfile);
       Rewrite(F2);
       n := 0;
     end;
  end;  
  close(F1);
  close(F2);
  writeln(outfile, ' created');
end.



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead
Hi,

Quote:

> What did I do wrong?

Instead of Reset(F1) and Rewrite(F2), call Reset(F1, 1) and
Rewrite(F2, 1). That's the record size, and since your "record" is a
byte, 1 is the correct size.

 - Sebastian

--
function HasAttachment : Boolean;
begin  { Does a posting have an attachment? }
   HasAttachment := Random(2) = 1;
end



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead


.................................
Quote:
>  Assign (F1, infile);
>  reset (F1);

.................................
Quote:
>  Assign (F2, outfile);
>  Rewrite(F2);

................................

These should be reset(F1, 1) and Rewrite(F2, 1) signaling that your
buffer is assigned 1 byte of data per array element.

Later you used Rewrite() for some reason and that will erase the file.



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Quote:
Sebastian Koppehel wrote...

>Hi,


>> What did I do wrong?

>Instead of Reset(F1) and Rewrite(F2), call Reset(F1, 1) and
>Rewrite(F2, 1). That's the record size, and since your "record" is a
>byte, 1 is the correct size.

Thanks a lot, Clif and Sebastian.

It was also on the Turbo Pascal Reference Guide, and I did not read
the examples carefully enough.



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead


[snip]

Quote:
>  Assign (F1, infile);
>  reset (F1);

should be reset(F1, 1); {otherwise record size of 128 is used}

Quote:
>  m := 1;
>  n := 0;
>  outfile := 'F01';
>  Assign (F2, outfile);
>  Rewrite(F2);
>  while not eof(F1) do
>  begin
>    BlockRead (F1, chunk, Sizeof(chunk), NumRead );

Here Pascal tries to read 51200*128 = $640000 bytes. Maybe the routine
in SYSTEM.TPU converts $640000 to word size and therefor tried
successfully reading 0 bytes.

Wolfgang

--
In order to e-mail me a reply to this message, you will have
to remove PLEASE.REMOVE from the address shown in the header.



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead


Quote:

>I'm trying to write a small TP5.5 program to split a large
>binary file (say, 30 MB) into 1.4 MB chunks.
>My problem was that BlockRead() didn't read from the input
>file (i.e. NumRead returned zero, see code below).

>What did I do wrong?

There is an example in the TP4 printed manual & TP7 on-line help for
BlockRead.  Reset & Rewrite need a second parameter, of 1, in this case.
Also, the example does not use EoF, and Help appears unclear as to the
effect of EoF on non-text files.

Copy the BlockRead example; see that it works; adapt it by adding what
you need.

As I now note that you're using TP5.5, and that the TP5.0 Help is less
helpful, here's the example :

  program CopyFile;
  var
    FromF, ToF: file;
    NumRead, NumWritten: Word;
    Buf: array[1..2048] of Char;
  begin
    Assign(FromF, ParamStr(1)); { Open input file }
    Reset(FromF, 1);  { Record size = 1 }
    Assign(ToF, ParamStr(2)); { Open output file }
    Rewrite(ToF, 1);  { Record size = 1 }
    Writeln('Copying ', FileSize(FromF), ' bytes...');
    repeat
      BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
      BlockWrite(ToF, Buf, NumRead, NumWritten);
    until (NumRead = 0) or (NumWritten <> NumRead);
    Close(FromF);
    Close(ToF);
  end.

There's another partial example, agreeing in essentials but better, in
TSFAQP #62; remember your {$M ; ISTM that it could be better to have the
buffer size a power of 2 or multiple of the sector size (try 32768).

Your NumRead is a word, so <= is nor needed, & = would do.

--

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



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Quote:




>>I'm trying to write a small TP5.5 program to split a large
>>binary file (say, 30 MB) into 1.4 MB chunks.
>>My problem was that BlockRead() didn't read from the input
>>file (i.e. NumRead returned zero, see code below).

>>What did I do wrong?

>There is an example in the TP4 printed manual & TP7 on-line help for
>BlockRead.

There's also an example at

http://www.deja.com/getdoc.xp?AN=631096564&fmt=text

which was posted earlier in response to his question.



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Quote:

>Sebastian Koppehel wrote...

>>Hi,


>>> What did I do wrong?

>>Instead of Reset(F1) and Rewrite(F2), call Reset(F1, 1) and
>>Rewrite(F2, 1). That's the record size, and since your "record" is a
>>byte, 1 is the correct size.

>Thanks a lot, Clif and Sebastian.

>It was also on the Turbo Pascal Reference Guide, and I did not read
>the examples carefully enough.

Did you read the earlier post which responded to your question
with working code illustrating this very point?

http://www.deja.com/getdoc.xp?AN=631096564&fmt=text



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Quote:

>I'm trying to write a small TP5.5 program to split a large
>binary file (say, 30 MB) into 1.4 MB chunks.
>My problem was that BlockRead() didn't read from the input
>file (i.e. NumRead returned zero, see code below).

>What did I do wrong?

Did you ever read the earlier reply which posted working
code for you to experiment with and modify??

http://www.deja.com/getdoc.xp?AN=631096564&fmt=text



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Clif says...

Quote:



>.................................
>>  Assign (F1, infile);
>>  reset (F1);
>.................................
>>  Assign (F2, outfile);
>>  Rewrite(F2);
>................................

>These should be reset(F1, 1) and Rewrite(F2, 1) signaling that your
>buffer is assigned 1 byte of data per array element.

He was told that in an earlier post.

http://www.deja.com/getdoc.xp?AN=631096564&fmt=text



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Quote:

>Hi,


>> What did I do wrong?

>Instead of Reset(F1) and Rewrite(F2), call Reset(F1, 1) and
>Rewrite(F2, 1). That's the record size, and since your "record" is a
>byte, 1 is the correct size.

He was told that in an earlier post.
http://www.deja.com/getdoc.xp?AN=631096564&fmt=text


Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Quote:


says...

> >I'm trying to write a small TP5.5 program to split a large
> >binary file (say, 30 MB) into 1.4 MB chunks.
> >My problem was that BlockRead() didn't read from the input
> >file (i.e. NumRead returned zero, see code below).

> >What did I do wrong?

> Did you ever read the earlier reply which posted working
> code for you to experiment with and modify??

Ok, ok, he gets it already!!!  Sheesh, 5 posts saying "read the answer
in a previous post" is enough!

--
Get paid to surf:
www.alladvantage.com/go.asp?refid=HAM962

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead

Quote:

> Did you ever read the earlier reply which posted working
> code for you to experiment with and modify??

> http://www.deja.com/getdoc.xp?AN=631096564&fmt=text

Why do you keep repeating this?


Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead
Hi,

Quote:

> He was told that in an earlier post.
> http://www.deja.com/getdoc.xp?AN=631096564&fmt=text

Obviously I didn't have or didn't see this reply when I posted my
message. What's so bad about getting the same reply more than once?
Five almost identical postings insisting that this answer was already
given are certainly worse.

Note that you and Stuart Dent seem to be using the same Usenet access
service, which might be the reason why you saw his article earlier
than some others.

 - Sebastian

--
function HasAttachment : Boolean;
begin  { Does a posting have an attachment? }
   HasAttachment := Random(2) = 1;
end



Wed, 18 Jun 1902 08:00:00 GMT  
 help need with BlockRead


Quote:




>>>I'm trying to write a small TP5.5 program to split a large
>>>binary file (say, 30 MB) into 1.4 MB chunks.
>>>My problem was that BlockRead() didn't read from the input
>>>file (i.e. NumRead returned zero, see code below).

>>>What did I do wrong?

>>There is an example in the TP4 printed manual & TP7 on-line help for
>>BlockRead.

>There's also an example at

>http://www.deja.com/getdoc.xp?AN=631096564&fmt=text

>which was posted earlier in response to his question.

As you have written too often already.

Please note that anonymous posters rarely, if ever, receive any respect
here.

--

 Web <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqn.zip> - Timo Salmi's Usenet Q&A.
 Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> :  about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

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

2. Q : Help with fpk and blockread

3. Help with BlockRead

4. Help with BlockRead

5. error 200 on blockread (w/ patched tpl files)

6. Blockread/Blockwrite question.

7. Blockread/Blockwrite question

8. BLOCKREAD&BLOCKWRITE?

9. Correct use of BlockRead or alternative loading approach

10. BlockRead

11. blockread and JAVA

12. Blockread/write, how fast?

 

 
Powered by phpBB® Forum Software