visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Pascal :: Restrictions on "file of byte" type


By: VB guy Canada  Date: 06/02/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
Hi,

I'm trying to write a sequence of 256 bytes, from 0 to 255, in any order, in a file of byte. Instead, after close the file I just got 64 bytes, but I was expecting 256. I've tried use Write, Blockwrite etc and the problem ever occurs. Can you help me?

Thanks.

VBguy


By: sumotimor Date: 06/02/2003 16:53:00 English  Type : Comment
Think above the file of byte like a PILE...
you must create the file and write a 256 "ceros" of byte...
and after that every byte can be access by position...
because the file is now 256 bytes on length...

Procedure Create25file;
Var I : Integer;
F : File of byte;
Begin
Reset(F,''); {<--- I forgot the command for create }
For A=1 to 256
Begin
Write(F,0);
End;
Close(F)
End;

Function ReadByteFile ( Poz : Integer ) : Byte;
Var Va : Byte;
Begin
Open(F,'');
Seek(F,Poz);
Read(F,Va);
Close(F);
ReadByteFile := Va;
End;

---------- This can helkp you


By: monange Date: 06/02/2003 17:02:00 English  Type : Comment
Post your code so we can look at it.
By: VGR Date: 07/02/2003 00:22:00 English  Type : Answer
I suspect that you read back your file of byte using an integer (or an other 4 bytes-format) because of the 64->256 discrepancy.

use the above comment from leirag, put
AssignFile(F,'yourfilenamehere');
ReWrite(F);

in stead of reset(); // forgot...

put := and "do" in "For a:=1 to 256 Do"

oh ok, better rewrite it myself :

Procedure Create25file;
Var I : Integer;
F : File of byte;
A : Byte;
Begin
AssignFile(F,'hereyourfilename');
ReWrite(F);
For A:=0 to 255 Do Write(F,0);
CloseFile(F);
End;

// use unsigned integers for type of "Poz" here. Byte would be enough, Word is better, you've LongWord too ;-)

Function ReadByteFile ( fn : String; Poz : Word ) : Byte;
Var Va : Byte;
Begin
AssignFile(F,fn);
{$I-} // or the reverse ;-)
ReSet(F);
{$I+}
if IOResult<>0 Then Begin Va:=0; WriteLn('invalid filename '+fn); End
Else Begin
Seek(F,Poz);
Read(F,Va);
Close(F);
End; // if, then, else
ReadByteFile := Va;
End;


By: VB guy Date: 07/02/2003 16:23:00 English  Type : Comment
Thank you all for the help.

But I want to do more explanations about the doubt.

I want to do the following:

var c1 : byte;
df : file of byte;
begin
assign(df,'Data.bin');

rewrite(df);

for c1:=0 to 255 do
write(df, c1);

closefile(df);
end;

Note that I want always write all 256 possible values of one byte.

And more. The simplification above don´t show, but the order of the 256 possible values of one byte may be random.

Thanks again.

By: sumotimor Date: 08/02/2003 18:04:00 English  Type : Comment
Yes after you have run the Create25File

you can read one byte from the file with function
ReadByteFile

And you can have a write changed a lien with Read--- for write... and add on argumments a byte to write and position

Ok... Thanks...VGR...

Tanks VGR again----->
By: VGR Date: 08/02/2003 18:22:00 English  Type : Comment
it worked ? :D
By: VB guy Date: 10/02/2003 11:42:00 English  Type : Comment
Yes, it works.
And all the others answers did that.
Thanks you all.

Regards.

VBguy


Do register to be able to answer

EContact
browser fav
page generated in 312.774900 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page