Go Premium for a chance to win a PS4. Enter to Win

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

read and write rtf file as binary

How can i read and write the contents of a richtextbox ?
0
isnoend2001
Asked:
isnoend2001
  • 11
  • 8
  • 3
  • +2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
 
isnoend2001Author Commented:
Yes that is what i have been using, however I don't want the file to be able to be opened with word
0
 
DhaestCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
isnoend2001Author Commented:
thanks Dhaest:, but that is written in C#, i am using vb6
0
 
DhaestCommented:
Reading and Writing Binary Files In Visual Basic
http://www.vb6.us/tutorials/reading-and-writing-binary-files-visual-basic
0
 
Genetic_WolfCommented:
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.
0
 
isnoend2001Author Commented:
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
0
 
LeithauserCommented:
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$
 
0
 
isnoend2001Author Commented:
Thanks Leithauser:
What data type would StringName$ and S$ be declared as ?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
LeithauserCommented:
<<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.
0
 
LeithauserCommented:
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
0
 
isnoend2001Author Commented:
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
0
 
LeithauserCommented:
DId you encrypt the file before saving it? If so, did you decrypt it where I said
'Decrypt the string here.
0
 
isnoend2001Author Commented:
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
0
 
LeithauserCommented:
The decryption would be
For X = 1 To Len(StringName$)
    Mid$(StringName$, X, 1) = Chr$(Asc(Mid$(StringName$, X, 1)) - 1)
Next X
 
0
 
isnoend2001Author Commented:
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
0
 
LeithauserCommented:
You need to change
S$ = Space$(LOF(1))
and
Rtf1.TextRTF = S$

to
StringName$ = Space$(LOF(1))
and
Rtf1.TextRTF = StringName$
In fact, do  search of the code I gave you for any reference to S$ and change it to StringName$.
When I was typing the code for you, I accidentally switched back and forth between S$%, which I normally use, and StringName$, which I thought was more descriptive.
 

0
 
isnoend2001Author Commented:
Thanks that works
0
 
LeithauserCommented:
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
0
 
isnoend2001Author Commented:
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.
0
 
Genetic_WolfCommented:
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.
0
 
isnoend2001Author Commented:
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
0
 
LeithauserCommented:
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.
0
 
isnoend2001Author Commented:
Leithauser:
your solution was just what i asked for, Thanks
0
 
Genetic_WolfCommented:

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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 8
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now