Writing text to file in binary mode

G

Gina Meyer

Hi all.
I am trying to write a string in binary mode so that when looking at the
file a human cannot read it ....
(is this the correct way at all to make it only machine readable ?)
Below is my function and it works fine when using 'For Output As #intFile'
..... when I use the two lines (***) together I get an error ....

where am I going wrong ?
Many thanks for your help!!

Gina

Public Function WriteComText(strVal As String)
Dim locComm as String
Dim f As Object
Dim bFileExists As Boolean
Dim intFile As Integer

intFile = FreeFile()

Set f = CreateObject("Scripting.FileSystemObject")

locComm = CurrentProject.Path & "\" & "Files"

bFileExists = f.FileExists(locComm & "\" & LCase("Commercial.txt"))

If bFileExists Then
Open locComm & "\" & LCase("Commercial.txt") For Output As #intFile
'Open locComm & "\" & LCase("Commercial.txt") For Binary Access
Write As #intFile 1***


If strVal <> "" Then
Print #intFile, strVal 2***
End If
Close #intFile

End If
End Function
 
G

Gina Meyer

Ok ... found a way from some help file ....

now I have a new question

Open locComm & "\" & LCase("Commercial.txt") For Random As #intFile
Len = Len(DSatz1)

Position = 1 .
Get #intFile, Position, DSatz1

How can I 'convert' the binary stuff to be visible as 'text' in order to
show the content to the user within a textbox of a form

before the binary stuff I used Line Input and read it into a strVariable
assigned to a textbox

Thanks Gina
 
G

Guest

How did you 'convert' the text to be 'binary stuff' in the file?

If you take text, and save it using Random, it is still text.
Writing using Binary or Random modes allows you to
write non-text characters, and to read non-text characters
like EOF, and to read past EOF characters, but it doesn't
convert anything to anything else.

(david)
 
G

Gina Meyer

Hi David

oh .. thanks so much for your answer ... especially between the years!!!
here's my code for Write & then Read
******
Public Function WriteComText(strVal as String)
Open locComm & "\" & LCase("Commercial.txt") For Random As #intFile Len
= Len(DSatz1)
DSatzNummer = 1

DSatz1.Kennung = 1 ' Kennung definieren.
DSatz1.Name = strVal & DSatzNummer ' Zeichenfolge erstellen.

Open locComm & "\" & LCase("Commercial.txt") For Output As #intFile

If strVal <> "" Then
'Put #1, DSatzNummer, DSatz1
End If
.....
....
End Function
*****

I would like to only see what is in the file (thought I would have to
convert the binary text somehow before I could read it as a person ?!!)
this is the original reading code and the '*** are the binary things..
Coukd I use the Line thing in combination with the binary then .... I can't
mix it ... I only get errors and do not know why and where to change what !

do I have to read in a file written in binary mode again into my code in
some kind of binary mode ???!! you see this is very unclear and I do not
know where to get some claryfying infos or a link ?!

*****
Public Function ReadComText() as String
.....
Dim DSatz1 As Datensatz, Position

intFile = FreeFile()
strComm = ""
i = 0

Set f = CreateObject("Scripting.FileSystemObject")
locComm = CurrentProject.Path & "\" & "Files"

bFileExists = f.FileExists(locComm & "\" & LCase("Commercial.txt"))

If bFileExists Then

Close #intFile
'**Open locComm & "\" & LCase("Commercial.txt") For Random As
#intFile Len = Len(DSatz1)

'**Position = 1
'**Get #intFile, Position, DSatz1 ' 3. Datensatz lesen.

Open locComm & "\" & LCase("Commercial.txt") For Input As #intFile

Do While Not EOF(intFile)
Line Input #intFile, Line
i = i + 1
If i = 1 Then
strComm = Line
Else
strComm= strComm & vbCrLf & Line
End If

Loop
Close #intFile
ReadComText = strComm
.......

End Function

******
Thanks again for your help !!!
Gina
 
G

Gina Meyer

Okay ... it works now with writing in Random mode and reading the lines in
Input mode ....
oh ... I am glad about this !!

what I am trying to do is to protect a file from viewing the contents and
editing it
Well, I know this maybe the wrong group to ask but is there a way to write
to a file in binary mode like in access-vba
my code now in access:
Open locComm & "\" & LCase("Commercial.txt") For Random As #intFile
Len = Len(DSatz1)
Put #intFile, DSatzNummer, DSatz1

in a ms-dos batch so that it is 'binary' ?? and the file cannot be edited
without my batch file!!! This is to protect the password file as I have set
up several different user access levels for my db

Gina
 
G

Guest

MS Windows/DOS has two ways of reading/writing files.
"Text" mode, and "Binary" mode.

"Text" mode is faster and easier, because it does not count
how long the file is, or where you are, or where you are
going, or how many characters you need to read. "Text" mode
just reads/writes the next characters until it gets to an EOF
character, and then stops.

"Binary" mode is slower and more complicated, because it
always keeps track of how long the file is, and where you
are, and how big your blocks are.

You need to use "binary" mode when copying binary files
(copy /b) because otherwise the copy will stop when it
comes to the first EOF character!

You need to use "binary" mode when reading binary files,
because otherwise the reading may stop when you come
to the first EOF character!

You need to use "binary" mode when using "Random",
because you need to keep track of where you are in the
file, and where you want to move to. "Text" mode only
ever lets you read the next character.

In computers, characters like "A" or "B" are represented
by numbers. (65 and 66 for A and B). You can write these
numbers using "Binary" mode or using "Text" mode. Unless
you write an EOF character (26), or need to write in a
different order, it makes no difference which mode you
use to write the numbers.

If you wish to read these numbers as characters, you can
open the file with a Text reader, like Notepad or Wordpad.

If you wish to read these numbers as numbers, you can
open the file with a Hex Editor.

If you encrypt or hash your passwords, you may then want
to read and write the strings using the binary mode, in case
one of the calculated values is an EOF character. Or, you
may wish to read and write the strings using the binary mode,
to help you find the password in the password file.

If you write to text to a file, it is still text in a file. You can
use either the simple mode, or the full service mode. The
read/write mode does not change the text.

(david)
 
G

Gina Meyer

David,

perfekt explanations - sure, you are a teacher - aren't you ???!!!
..... huuu this stuff seems to come out far more complicated than I first
thought ....

Had a further play with these things yesterday .... and it works now fine
( after a few hours of trial & error !)
together with your information I will have to think it through what I am
going to do with my files

copy/b .... going to try it out ....

hey .... I am very grateful for your extensive information and all the time
you spend on my problem...
many thanks .... have a Happy New Year!!!!

Gina
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top