whitespace in txt file ignored until I resave file in Notepad

TomGroves used Ask the Experts™
I’m experiencing a weird problem.  I’m using VB 6 to create a small application that reads an automatically-generated plain-text table with 7 fields and will output the data in XML.  Each field is a pre-determined length with white space filling the unused portion of the fields.  This is the type of thing most of us have done many times, but I’m encountering a strange error with this project.

This problem is this: When the program attempts to read the target file, the ‘white space’ is ignored and the fields don’t divide correctly.  However, if I open the file with Notepad the fields are displayed in an orderly semi-table- using the white space to fill the unused portion of each field.  What is strange is that if I then save the file in Notepad (without making any changes to the file), and then process the newly saved file from within my program, it works perfectly!  The properties page shows the two files to be identical- down to the last byte- but only the resaved file will work.  

Within VB, I’ve used the FileSystemObject style and also the Open command to open and read the text; both work great on the resaved file, but neither can ‘see’ the white space in the original file.  

Does anyone have any thoughts on this problem?  Notepad seems to be doing something to the file, but what could that be?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think you've probably got a null character (hex 00) before the white space.  Notepad probably rewrites this.  Use a hex editor to view the file before Notepad opens/saves it and then after.  Compare the bytes.


You were right about the null characters.  Before Notepad the fields are filled with ‘00’ (null) chrs, and after saving in Notepad these are replaced with ‘20’s.  This is the equivalent of ASCII 32- a space.  I’m still having trouble getting VB to ‘read-in’ the null chars.  Here is the snippet I’m using to try and convert the characters:

Dim MyChar
Open "file.log" For Input As #1   ' Open file.
Do While Not EOF(1)   ' Loop until end of file.
   MyChar = Input(1, #1)   ' Get one character.
   If MyChar = Asc(0) Then
        MyChar = "!"
   End If
   Text1.Text = Text1.Text & MyChar & "/" & Asc(MyChar) & "    "

Here I’m trying to detect the nulls, but by the time the text file gets to this point the nulls have been stripped out of the file, leaving the fields incorrectly spaced.  How on earth could I preserve the spacing when VB insists on disregarding any null character it sees?
Here's what you want:

    Dim MyFile As String
    Dim lFreefile As Long
    lFreefile = FreeFile
    Open "c:\mywhateverfile.txt" For Binary As #lFreefile  'Opens The File And Names It #1
    MyFile = Input$(LOF(lFreefile), lFreefile)   'Assigns The Text In The File To MyFileText
    MyFile = Replace(MyFile, Chr$(0), "!")
    Text1.Text = MyFile
    Close #lFreefile

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial