isnoend2001
asked on
read and write rtf file as binary
How can i read and write the contents of a richtextbox ?
ASKER
Yes that is what i have been using, however I don't want the file to be able to be opened with word
Reading/writing binary files
http://www.java2s.com/Code/ASP/File-Directory/ReadwritebinaryfileC.htm
http://www.java2s.com/Code/ASP/File-Directory/ReadwritebinaryfileC.htm
ASKER
thanks Dhaest:, but that is written in C#, i am using vb6
Reading and Writing Binary Files In Visual Basic
http://www.vb6.us/tutorials/reading-and-writing-binary-files-visual-basic
http://www.vb6.us/tutorials/reading-and-writing-binary-files-visual-basic
The RTF (Rich Text Format) Will ALWAYS be readable by word, no matter what.
RTF format was the FIRST format Microsoft used in Word 1.0
howerver, if you want to prevent your user to Double click on a file and open it,
just use a different extention.
save it for exemple as: myFile.rtz
if user are double clicking the file, Microsoft will ask with what the user want to open it.
but if the user, chose Word as opening device, or if user open Word, and choose file/Open and select your file, no matter how you recorded it, if it's RTF, Word will open it.
if you need a more solid method to prevent your user to open your RTF, you encapsulate your file into a different mime type and then binary save it. You'll need to reverse the process in order to see the file in your program though.
if you explain to us the goal of this, maybe we can select a better solution for you.
if you're only trying to hide a msg and you'll NOT need to edit that message after, you would be better to create an image of the message. That's more easy to hide.
RTF format was the FIRST format Microsoft used in Word 1.0
howerver, if you want to prevent your user to Double click on a file and open it,
just use a different extention.
save it for exemple as: myFile.rtz
if user are double clicking the file, Microsoft will ask with what the user want to open it.
but if the user, chose Word as opening device, or if user open Word, and choose file/Open and select your file, no matter how you recorded it, if it's RTF, Word will open it.
if you need a more solid method to prevent your user to open your RTF, you encapsulate your file into a different mime type and then binary save it. You'll need to reverse the process in order to see the file in your program though.
if you explain to us the goal of this, maybe we can select a better solution for you.
if you're only trying to hide a msg and you'll NOT need to edit that message after, you would be better to create an image of the message. That's more easy to hide.
ASKER
Thanks Genetic_Wolf:
I have already changed the extension, but it can still be viewed in word and wordpad. Think i will try to put it into a udt and add some fake fields, but i still need a way to save and read as binary
I have already changed the extension, but it can still be viewed in word and wordpad. Think i will try to put it into a udt and add some fake fields, but i still need a way to save and read as binary
If you do not want people to be able to load the file in word, you need to encrypt the file. You can get the full content of the Rich Text Box with RichTextBoxName.TextRTF, where RichTextBoxName is the name of your file. For Example,
StringName$ = RichTextBox1.TextRTF
stores the entire contents of RichTextBox1, including formating, in StringName$. Then encrypt this string. It can be something as simple as
For X = 1 to Len(StringName$)
Mid$(StringName$,X,1) = Chr$(Asc(Mid$(StringName$, X,1)) +1)
Next X
You can then save the string with something like
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
(You might want to Kill the file first if it already exists.)
You can then recover the file with
Open "FileName" For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
'Decrypt the string here.
RichTextBox1.TextRTF = S$
StringName$ = RichTextBox1.TextRTF
stores the entire contents of RichTextBox1, including formating, in StringName$. Then encrypt this string. It can be something as simple as
For X = 1 to Len(StringName$)
Mid$(StringName$,X,1) = Chr$(Asc(Mid$(StringName$,
Next X
You can then save the string with something like
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
(You might want to Kill the file first if it already exists.)
You can then recover the file with
Open "FileName" For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
'Decrypt the string here.
RichTextBox1.TextRTF = S$
ASKER
Thanks Leithauser:
What data type would StringName$ and S$ be declared as ?
What data type would StringName$ and S$ be declared as ?
The '$' is old-school shorthand declaration for the String data type.
Here is the actual MS documentation:
http://support.microsoft.com/default.aspx?scid=kb;en-us;191713
% Integer
& Long
! Single
# Double
$ String
@ Currency
Here is the actual MS documentation:
http://support.microsoft.com/default.aspx?scid=kb;en-us;191713
% Integer
& Long
! Single
# Double
$ String
@ Currency
<<What data type would StringName$ and S$ be declared as>>
String. As mentioned my Idle Mind, adding a $ to the end of a varialbe name is an old (but still valid) way of specifying that a variable is a string. It predates the Dim, Declare, Public, Private, and Global keywords.
String. As mentioned my Idle Mind, adding a $ to the end of a varialbe name is an old (but still valid) way of specifying that a variable is a string. It predates the Dim, Declare, Public, Private, and Global keywords.
BTW, the variable should be either S$ or StringName$ or whatever. I got a little confused in my typing and accidentally used S$ in some places and StringName$ in others. It should always be hte same variable name. That is,
Open "FileName" For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
should be
Open "FileName" For Binary As 1
StringName$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
Open "FileName" For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
should be
Open "FileName" For Binary As 1
StringName$ = Space$(LOF(1))
Get #1, 1, StringName$
Close 1
ASKER
Leithauser:
The file cannot be read by word, however it does not load into the rtf either, maybe i am doing something wrong:
Sub OpenRTF()
Dim fName As String
Dim fnumber As Integer
fnumber = FreeFile
fName = App.Path & "\Spellcheck.dat"
Open fName For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close #1
'Decrypt the string here.
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) + 1)
Next X
Rtf1.TextRTF = S$
End Sub
The file cannot be read by word, however it does not load into the rtf either, maybe i am doing something wrong:
Sub OpenRTF()
Dim fName As String
Dim fnumber As Integer
fnumber = FreeFile
fName = App.Path & "\Spellcheck.dat"
Open fName For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close #1
'Decrypt the string here.
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$,
Next X
Rtf1.TextRTF = S$
End Sub
DId you encrypt the file before saving it? If so, did you decrypt it where I said
'Decrypt the string here.
'Decrypt the string here.
ASKER
Yes i Encrypted before saving and does this not decrypt the string:
'Decrypt the string here.
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) + 1)
Next X
as the code above
'Decrypt the string here.
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$,
Next X
as the code above
The decryption would be
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) - 1)
Next X
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$,
Next X
ASKER
Leithauser: I changed the decryption, but still a blank rtf
Sub OpenRTF()
'Dim sText As String
Dim fName As String
Dim fnumber As Integer
fnumber = FreeFile
fName = App.Path & "\Spellcheck.dat"
Open fName For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close #1
'Decrypt the string here.
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) - 1)
Next X
Rtf1.TextRTF = S$
End Sub
Sub OpenRTF()
'Dim sText As String
Dim fName As String
Dim fnumber As Integer
fnumber = FreeFile
fName = App.Path & "\Spellcheck.dat"
Open fName For Binary As 1
S$ = Space$(LOF(1))
Get #1, 1, StringName$
Close #1
'Decrypt the string here.
For X = 1 To Len(StringName$)
Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$,
Next X
Rtf1.TextRTF = S$
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks that works
Thanks for the points. I need to make 3000 points per month to maintain my own free unlimited question points, plus the fact that I am now just 3,306 more points until Master rating. One more question this month will maintain my free question points, and two more should get me my Masters rating. :-)
BTW, I think if you use
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
to write a shorter string when you had prevuiously a longer one in that file, you MAY leave trailing characters from your original longer string on the new one in the file (not sure about this). This is why I suggested that you kill the file before writing to it again. You can do this with
If Len(Dir$("FileName")) Then
Kill "FileName"
End If
before
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
BTW, I think if you use
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
to write a shorter string when you had prevuiously a longer one in that file, you MAY leave trailing characters from your original longer string on the new one in the file (not sure about this). This is why I suggested that you kill the file before writing to it again. You can do this with
If Len(Dir$("FileName")) Then
Kill "FileName"
End If
before
Open "FileName" For Binary As 1
Put #1, 1, StringName$
Close 1
ASKER
Thanks for adding that. If your last fix did not work I was going to go to Renta Coder and pay to have it written as i have about 50 times before.. You saved me $.
I am writing in xp and this file is for my son who uses Win7. win7 does not like using App.path so some rewriting, setup and manifest is next.
I am writing in xp and this file is for my son who uses Win7. win7 does not like using App.path so some rewriting, setup and manifest is next.
still readable in Word though...
the encrypt function is not very solid all you have to do is substract 1 letter from what you read.
and if you got a caracter 255, the function will crash.
if you really need a security function, tell me.
the encrypt function is not very solid all you have to do is substract 1 letter from what you read.
and if you got a caracter 255, the function will crash.
if you really need a security function, tell me.
ASKER
Thanks Genetic_Wolf, but for this purpose it works as everything is in images. Looks like this:
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd
e9f:fde9f:fde9f:fde9f:fde9 f:fdee9886 de857ecd64 5ggggggg11 gggggggggg gged9185ee 9d
86ed9d86e9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fd111111e9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fd1111 11e9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fd11111111 1111111111 e9f:fde9f: fde9f:fd11 11
11111111111111e9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:
fde9f:fde9f:fde9f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fdee98 86de857ecd 64
5ggggggg11gggggggggggged91 85ee9d86ed 9d86e9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
f:fde9f:fde9f:fde9f:fde9f: fde9f:fde9 f:fde9f:fd e9f:fde9f: fde9f:fde9 f:fde9f:fd e9
If i need something stronger i found Platform SDK Redistributable: CAPICOM
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
e9f:fde9f:fde9f:fde9f:fde9
86ed9d86e9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
11111111111111e9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
fde9f:fde9f:fde9f:fde9f:fd
5ggggggg11gggggggggggged91
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
f:fde9f:fde9f:fde9f:fde9f:
If i need something stronger i found Platform SDK Redistributable: CAPICOM
Note that the encryption technique I gave is very simple, and was really just intended as an example of a simple encryption system, not a definitive solution. I figured you would want to write your own, so that only you would know it. Otherwise, anyone looking at Experts-Exchange would know your encryption.
In addition, it appears from your question that you were not really looking for a high security solution, just something that would stop the casual user from loading the file in Word. Since RTF relies on a rather rigid set of coding to interpret the file formatting, changing or rearranging even a few characters makes it unreadable in Word.
In addition, it appears from your question that you were not really looking for a high security solution, just something that would stop the casual user from loading the file in Word. Since RTF relies on a rather rigid set of coding to interpret the file formatting, changing or rearranging even a few characters makes it unreadable in Word.
ASKER
Leithauser:
your solution was just what i asked for, Thanks
your solution was just what i asked for, Thanks
I was just pointing out that it was VERY far from the original question asked.
and if user give more explanation at what he intend to do or what goal he want to achieve, Expert like us can sometime give a more appropriate answer.
but I'm glad the solution satisfy him, good work.
http://msdn.microsoft.com/en-us/library/aa261642(VS.60).aspx
http://msdn.microsoft.com/en-us/library/aa261644(VS.60).aspx