Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving and restoring data to a text file (with a twist)

Status
Not open for further replies.

VB400

Programmer
Sep 8, 1999
359
US
I have the need to write a single string to a text file and later retrieve that text into a variable -- sounds simple, but...<br>
I can do this with simple code using &quot;open&quot;, &quot;print #&quot; and &quot;close&quot; followed by &quot;open&quot;, &quot;input #&quot; and &quot;close&quot;. This works well depending on the data that is in the string variable.<br>
<br>
In my case, the string is a representation of a propertybag's contents. When I write the string to the text file, it includes various characters that are generated by propertybag. When I read the text file into a string variable, I lose some of those characters (mainly the chr(0) character). Here's a simple application that you can copy into a module and run to see what I'm talking about (change the project startup to Sub Main):<br>
<br>
Sub main()<br>
<br>
Dim pb As PropertyBag<br>
Dim s As String<br>
<br>
Set pb = New PropertyBag<br>
pb.WriteProperty &quot;Item&quot;, &quot;ABCDEFG&quot;<br>
s = pb.Contents<br>
Set pb = Nothing<br>
<br>
'Output to text file<br>
Open &quot;c:\testing.txt&quot; For Output As #1<br>
Print #1, s<br>
Close #1<br>
Debug.Print s<br>
<br>
'Input from text file<br>
s = &quot;&quot;<br>
Open &quot;c:\testing.txt&quot; For Input As #1<br>
Line Input #1, s<br>
Debug.Print s<br>
Close #1<br>
<br>
End Sub<br>
<br>
The result is this (notice how the blanks are gone -- they're actually chr(0)s not blanks):<br>
? 0 ?? item ABCDEFG <br>
?0??itemABCDEFG<br>
<br>
Any Ideas?
 
I made the following changes to your code, placing same into<br>
a Command Button to click on. The program runs fine for<br>
me without any errors. Will this modification work for you ?<br>
<br>
Jim - <A HREF="mailto:jdmcjen@stc.net">jdmcjen@stc.net</A><br>
<br>
Private Sub Command1_Click()<br>
'Dim pb As PropertyBag<br>
Dim s As String<br>
<br>
'Set pb = New PropertyBag<br>
'pb.WriteProperty &quot;Item&quot;, &quot;ABCDEFG&quot;<br>
s = &quot;ABCDEFG&quot; 'pb.Contents<br>
'Set pb = Nothing<br>
<br>
'Output to text file<br>
Open &quot;c:\testing.txt&quot; For Output As #1<br>
Print #1, s<br>
Close #1<br>
Debug.Print s<br>
<br>
'Input from text file<br>
s = &quot;&quot;<br>
Open &quot;c:\testing.txt&quot; For Input As #1<br>
Line Input #1, s<br>
Debug.Print s<br>
Close #1<br>
End Sub<br>

 
Thanks Logit<br>
<br>
The modification you made works for strings. Im my case, the data to be saved is originated by<br>
PropertyBag.Contents which is a byte array not a string.
 
A couple of suggestions:<br>
1. Open the file for BINARY and see if that makes a difference.<br>
<br>
2. Traverse the string 's' and replace each occurrence of chr(0) with say &quot;CHRZERO&quot; then write the file. When you read the string in you change &quot;CHRZERO&quot; back to chr(0).<br>
e.g. <br>
<br>
dim tempS as string, i as integer<br>
for i = 1 to len( s)<br>
if mid(s, i, 1) = chr(0) then<br>
tempS = tempS & &quot;CHRZERO&quot;<br>
else<br>
tempS = mid(s, i, 1)<br>
next i<br>
Print #1, tempS<br>
<br>
Do the reverse when you read the string back.<br>
You can then include this solution for any character that gives you a problem.<br>
<br>
NOTE: i have not tested this code. It looks ok but it may have bugs.<br>
<br>
HTH<br>
johncb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top