Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

read and write rtf file as binary

How can i read and write the contents of a richtextbox ?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Avatar of isnoend2001

ASKER

Yes that is what i have been using, however I don't want the file to be able to be opened with word
thanks Dhaest:, but that is written in C#, i am using vb6
Avatar of Genetic_Wolf
Genetic_Wolf

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.
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
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$
 
Thanks Leithauser:
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
<<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.
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
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
DId you encrypt the file before saving it? If so, did you decrypt it where I said
'Decrypt the string here.
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
The decryption would be
For X = 1 To Len(StringName$)
    Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) - 1)
Next X
 
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
ASKER CERTIFIED SOLUTION
Avatar of Leithauser
Leithauser

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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.
Thanks Genetic_Wolf, but for this purpose it works as everything is in images. Looks like this:
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd
e9f:fde9f:fde9f:fde9f:fde9f:fdee9886de857ecd645ggggggg11gggggggggggged9185ee9d
86ed9d86e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd111111e9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fd111111e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fd111111111111111111e9f:fde9f:fde9f:fd1111
11111111111111e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:
fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fdee9886de857ecd64
5ggggggg11gggggggggggged9185ee9d86ed9d86e9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9
f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9f:fde9

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.
Leithauser:
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.