Reading binary data from a file and writing it

Reply

Join Date: Dec 2005
Posts: 109
Reputation: complete is an unknown quantity at this point 
Solved Threads: 0
complete's Avatar
complete complete is offline Offline
Junior Poster

Reading binary data from a file and writing it

 
0
  #1
Jan 2nd, 2006
In Visual Basic 6 I have written a program to read and write to a file. It works fine. Now I have to do the same for binary data since the information I write to the file has end-of-line and return characters. For instance, when I write to the file, the data looks like this:

"3
34"

Because there is a break in the data. In debug mode, the text data has two boxes where the end-of-line and return characters go.

Anyway, does anyone have any example code of binary level reading and writing to and from a file?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Reading binary data from a file and writing it

 
0
  #2
Jan 4th, 2006
The character codes for that are vbcr and vblf (also vbcrlf) which translate to chr(13) and chr(10) (those are carriage return and line feed). So, if it would be easier for you to merely check for an occurance of either of those in a string using instr, and replacing the offending character with the replace function, that might be an easier solution for you. However, if you want to leave the file completely unchanged, and merely write the file as it is with binary:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim B() As Byte
  2. Dim srcFile as string
  3. dim destFile as string
  4.  
  5. ' /* Change This To your File */
  6. srcFile = "c:\somefile.exe"
  7. destFile = "c:\destination.exe"
  8.  
  9. Open srcFile For Binary Access Read As #1
  10. ReDim B(LOF(1) - 1)
  11. Get #1, , B
  12. Close #1
  13.  
  14. open destfile for binary access write as #1
  15. put #1, , B
  16. close #1

Let me know what you come up with.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC