Recent

Author Topic: write data into binary file  (Read 7004 times)

opera

  • Newbie
  • Posts: 6
write data into binary file
« on: November 26, 2013, 08:32:24 am »
if I want to write integer 100 and double 123.456 into a binary file.do I need to first assign-rewrite-blockwrite-close to write 100 and then again assign-rewrite-blockwrite-close to write 123.456? is there concise method? thank you.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: write data into binary file
« Reply #1 on: November 26, 2013, 08:38:20 am »
pseudo code
assignfile();
rewrite(file);
blockwrite(myinteger,sizeof(myinteger));
blockwrite(myDouble,sizeof(myDouble));
blockwrite(length(myString), sizeof(integer));
blockwrite(myString[1],length(myString));

Keep on using blockwrite as needed then close the file.
« Last Edit: November 26, 2013, 08:39:51 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: write data into binary file
« Reply #2 on: November 26, 2013, 09:08:41 am »
Thanks, taazz!

opera

  • Newbie
  • Posts: 6
Re: write data into binary file
« Reply #3 on: November 26, 2013, 09:16:24 am »
pseudo code
assignfile();
rewrite(file);
blockwrite(myinteger,sizeof(myinteger));
blockwrite(myDouble,sizeof(myDouble));
blockwrite(length(myString), sizeof(integer));
blockwrite(myString[1],length(myString));

Keep on using blockwrite as needed then close the file.

I write the following code:

program test;
var
f:file;
a:integer;
b:double;
begin
a:=100;
b:=123.456;
assign(f,'testfile') ;
rewrite(f);
blockwrite(f,a,sizeof(a));
blockwrite(f,b,sizeof(b));
close(f);
end.

after run, the testfile is 1280 bytes,what is wrong with it?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: write data into binary file
« Reply #4 on: November 26, 2013, 09:29:30 am »
Source :
http://www.freepascal.org/docs-html/rtl/system/rewrite.html

From the description you need to do something like.
Code: [Select]
program test;
var
  f:file;
  a:integer;
  b:double;
begin
  a:=100;
  b:=123.456;
  assign(f,'testfile') ;
  rewrite(f,1);//<------ changed to 1 byte block instead of 128 bytes that is the default.
  blockwrite(f,a,sizeof(a));
  blockwrite(f,b,sizeof(b));
  close(f);
end.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

opera

  • Newbie
  • Posts: 6
Re: write data into binary file
« Reply #5 on: November 26, 2013, 09:34:22 am »
thank you, taazz

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: write data into binary file
« Reply #6 on: November 27, 2013, 12:47:31 am »
Should you use a Try-Finally around the whole Assign-to-Close block or per line so you know what failed?
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

 

TinyPortal © 2005-2018