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 "open", "print #" and "close" followed by "open", "input #" and "close". 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 "Item", "ABCDEFG"<br>
s = pb.Contents<br>
Set pb = Nothing<br>
<br>
'Output to text file<br>
Open "c:\testing.txt" For Output As #1<br>
Print #1, s<br>
Close #1<br>
Debug.Print s<br>
<br>
'Input from text file<br>
s = ""<br>
Open "c:\testing.txt" 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 can do this with simple code using "open", "print #" and "close" followed by "open", "input #" and "close". 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 "Item", "ABCDEFG"<br>
s = pb.Contents<br>
Set pb = Nothing<br>
<br>
'Output to text file<br>
Open "c:\testing.txt" For Output As #1<br>
Print #1, s<br>
Close #1<br>
Debug.Print s<br>
<br>
'Input from text file<br>
s = ""<br>
Open "c:\testing.txt" 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?