For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > December 2005 > (Basic) Create binary file problem.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author (Basic) Create binary file problem.
Boki

2005-12-15, 3:55 am

Hi All,
When I put my jpg byte array to picturebox by the method in last
thread, I can see the picture was change, but nothing shown. ( no pictures,
and the original picture will be removed )

Now, I want to write those data to disk and open it by other program.

I have tried :

=====================
Open "c:\data.dat" For Binary As #1
For a = 0 To 100
Put #1, a, Chr(123)
Next

Close #1
=====================
Of course, you should know where is wrong, but I don't know...

How can I assign the put # 1...... to save data to disk in binary (HEX)
format ?

Thank you very much!


Best regards,
Boki.


J French

2005-12-15, 7:55 am

On Thu, 15 Dec 2005 16:50:27 +0800, "Boki" <bokiteam@ms21.hinet.net>
wrote:

>Hi All,
> When I put my jpg byte array to picturebox by the method in last
>thread, I can see the picture was change, but nothing shown. ( no pictures,
>and the original picture will be removed )
>
>Now, I want to write those data to disk and open it by other program.
>
>I have tried :
>
>=====================
>Open "c:\data.dat" For Binary As #1
>For a = 0 To 100
>Put #1, a, Chr(123)
>Next
>
>Close #1
>=====================
>Of course, you should know where is wrong, but I don't know...


When a is zero, this should fail

As it is, if you fixed that, you would just write a load of Chr$(123)s


>How can I assign the put # 1...... to save data to disk in binary (HEX)
>format ?


HEX is not really Binary

Do you really want to save the picture in Hex ?

Not many things will be able to read it

To write a Byte Array directly to file

Channel = FreeFile
Open "some.jpg" for Binary As #Channel
Put# Channel, 1, B()
Close# Channel

To write a load of Hex from a Byte Array B()

Open "some.jpg" for Binary As #Channel
For L9 = LBound( B() ) To UBound( B() )
Put# Channel, , Hex$( B( L9 ) )
Next
Close# Channel

Better explain what you really want to do.
Boki

2005-12-15, 6:55 pm

Thank you very much! Now, I know how to write HEX, but I think I have to use
binary as corre.

Here is my new question:
My code:
=======================================
Open "c:\test.jpg" For Binary As #1
Put #1, 1, JPEG_Buffer
Close #1
=======================================
JPEG_Buffer is a byte array.

Q1. I found the "test.jpg" will be coverd again and again, if I want to
create a whole new file, what should I do?

Q2. Could you please adivce, if it is available in VB6:

Put #1, 1, JPEG_Buffer(3)

I have tried, but it seems no work... ><


Thank you very much for your help.

Best regards,
Boki.




"J French" <erewhon@nowhere.uk> 撰寫於郵件新:43a155b9.524369302@news.btopenworld.com...
> On Thu, 15 Dec 2005 16:50:27 +0800, "Boki" <bokiteam@ms21.hinet.net>
> wrote:
>
>
> When a is zero, this should fail
>
> As it is, if you fixed that, you would just write a load of Chr$(123)s
>
>
>
> HEX is not really Binary
>
> Do you really want to save the picture in Hex ?
>
> Not many things will be able to read it
>
> To write a Byte Array directly to file
>
> Channel = FreeFile
> Open "some.jpg" for Binary As #Channel
> Put# Channel, 1, B()
> Close# Channel
>
> To write a load of Hex from a Byte Array B()
>
> Open "some.jpg" for Binary As #Channel
> For L9 = LBound( B() ) To UBound( B() )
> Put# Channel, , Hex$( B( L9 ) )
> Next
> Close# Channel
>
> Better explain what you really want to do.



Richard Jalbert

2005-12-15, 6:56 pm

On Fri, 16 Dec 2005 00:38:00 +0800, "Boki" <bokiteam@ms21.hinet.net>
wrote:

>Thank you very much! Now, I know how to write HEX, but I think I have to use
>binary as corre.
>
>Here is my new question:
>My code:
>=======================================
> Open "c:\test.jpg" For Binary As #1
> Put #1, 1, JPEG_Buffer
> Close #1
>=======================================
>JPEG_Buffer is a byte array.
>
>Q1. I found the "test.jpg" will be coverd again and again, if I want to
>create a whole new file, what should I do?
>
>Q2. Could you please adivce, if it is available in VB6:
>
>Put #1, 1, JPEG_Buffer(3)
>
>I have tried, but it seems no work... ><


I think this would only put the value of the fourth byte Iif your
array is zero based) in a binary file, at position 1 in that file.

Try
Dim intFilenum as Integer
intFilenum = FreeFile
Open "Newfilename" For Binary As #intFilenum
Put #intFilenum, 1, JPEG_Buffer
' This will write the whole buffer to the new file
Close #intFilenum

>Thank you very much for your help.
>
>Best regards,
>Boki.
>
>
>
>
>"J French" <erewhon@nowhere.uk> 撰寫於郵件新:43a155b9.524369302@news.btopenworld.com...
>
>


****************************************
******************************
Richmann@sympatico.ca

Dog thinks: they feed me, they take care of me: they are gods.
Cat thinks: they feed me, they take care of me: I am god.
http://www3.sympatico.ca/richmann/
http://www.geocities.com/richmannsoft/
****************************************
******************************
Boki

2005-12-15, 9:55 pm

Thanks a lot. I will try it.

For Q1, I update my description:
==================================
Put #1, 1, &JPEG_Buffer(3) '// can I do this kind of assignment?
==================================


Best regards,
Boki.


"Richard Jalbert" <richmann@sympatico.ca> 撰寫於郵件新聞:43a1d926.26003406@news1.qc.sympatico.ca...
> On Fri, 16 Dec 2005 00:38:00 +0800, "Boki" <bokiteam@ms21.hinet.net>
> wrote:
>
>
> I think this would only put the value of the fourth byte Iif your
> array is zero based) in a binary file, at position 1 in that file.
>
> Try
> Dim intFilenum as Integer
> intFilenum = FreeFile
> Open "Newfilename" For Binary As #intFilenum
> Put #intFilenum, 1, JPEG_Buffer
> ' This will write the whole buffer to the new file
> Close #intFilenum
>
>
> ****************************************
******************************
> Richmann@sympatico.ca
>
> Dog thinks: they feed me, they take care of me: they are gods.
> Cat thinks: they feed me, they take care of me: I am god.
> http://www3.sympatico.ca/richmann/
> http://www.geocities.com/richmannsoft/
> ****************************************
******************************



Boki

2005-12-15, 9:55 pm


> Try
> Dim intFilenum as Integer
> intFilenum = FreeFile
> Open "Newfilename" For Binary As #intFilenum
> Put #intFilenum, 1, JPEG_Buffer
> ' This will write the whole buffer to the new file
> Close #intFilenum




Hi,

It seems no work.

If I have a old file, for example 2048Byte - 'A'

If I open that file and write data that is less than 2048Byte, ex 1024byte -
'B'

The final data of file will become

1024 x 'b' + 1024 x 'a' ....

right ?

Best regards,
Boki.


J French

2005-12-16, 7:55 am

On Fri, 16 Dec 2005 10:47:46 +0800, "Boki" <bokiteam@ms21.hinet.net>
wrote:

>Hi,
>
>It seems no work.
>
>If I have a old file, for example 2048Byte - 'A'
>
>If I open that file and write data that is less than 2048Byte, ex 1024byte -
>'B'
>
>The final data of file will become
>
>1024 x 'b' + 1024 x 'a' ....
>
>right ?


Yes - Binary does not truncate the file

Try this :-

Private Sub Command1_Click()
Const Fle = "c:\t\test.dat"
Dim B() As Byte
Dim Channel&, L9&, S$

ReDim B(10)
For L9 = 0 To UBound(B())
B(L9) = L9
Next

' Truncate the File
Channel = FreeFile
Open Fle For Output As #Channel
Close #Channel

' Write the Data
Channel = FreeFile
Open Fle For Binary As #Channel
Put #Channel, 1, B()
Close #Channel

End Sub

It should be pretty fast as the file name will definitely exist on the
second Open - and there are numerous buffers/caches active.

Checking for, and deleting the file first is another option, but that
should be slower as it could cause a fair bit of directory activity.

There is also an API for truncating the file, I doubt it would be any
faster and it is a bit messy.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com