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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read file with NULL characters?

Status
Not open for further replies.

tb2007

Programmer
Apr 2, 2005
33
YU
In my Vb app. I need to read Test.txt file which has large amount of formated text and sometimes NULL characters.


So, for testing I made this file like this:

s = "h" & vbNullChar & "e" & vbNullChar & "l" & vbNullChar & "p" & vbNullChar
n = FreeFile()
Open "c:\Test.txt" For Output As #n
Print #n, s
Close #n

Now in VB I read this file like this:


n = FreeFile()
Open "c:\Test.txt" For Input As #n
Text = Input(LOF(n), n)
Close #n

But I got error: Input past end of file 62.


Ok, I tried this way.

RichTextBox.LoadFile "c:\Test.txt", 1

In my RichTextBox I see: “h_e_l_p_” where “_” is NULL character

Then I say:

Text=RichTextBox.Text

But variable Text got this value:

Text = ”h”


How to read the whole formated Test.txt with NULL characters?
 
Hi

Just a suggestion to follow.

Since you appear to read the file into a RichTextBox okay, why not use the Replace() function to replace the nulls with some character that VB will not interrupt and that will not show up in the original text? For example,

Code:
    strReplacedText = Replace(RichTextBbox, vbnull, chr(182),1,,vbBinaryCompare)

I haven't tried this, but suggest it as a possibility.


Cassie
PIII-500Mhz-128MB
Win98SE-VB6SP5
 
If your file is known to contain NUL characters, your best option is to read it as a binary file, and convert the bytes to a string.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top