[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

Binary file access

I need to write some strings, line by line into a binary file, and later read back the same strings line by line from the same file.
I tried the following, but failed, can someone help?

Open "a.txt" For Binary Access Write As #1
 Write #1, "data1"
 Write #1, "data2"
Close #1
   
Open "a.txt" For Input Access Read As #1
   
Do While Not EOF(1)
   Line Input #1, TextLine
   'some process to store the string
Loop
   
Close #1
0
recce
Asked:
recce
1 Solution
 
cvidlerCommented:
When writing to a binary file you MUST include a vbCrLf when you want a new line to start. Also you should use variables to write to the file not constants.  eg.


open "a.txt" for binary access write as #1

MyVar$ = "data 1" + vbCrLf
put #1, , MyVar$
MyVar$ = "data 2" + vbCrLf
Put #1, , MyVar$

Close #1


Open "a.txt" For Input Access Read As #1
     
Do While Not EOF(1)
   Line Input #1, TextLine
   'some process to store the string
Loop
     
Close #1


PS: Why are you writing to a binary file if it is going to be a text file - you should open it as follows

Open "a.txt" For Output Access Write As #1

then you can use the code you have above! No need to worry about adding vbCrLf to each line...
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now