Three extra characters when some files are read, why?

mdlueck

Sr. Application Developer
Local time
Today, 18:24
Joined
Jun 23, 2011
Messages
2,650
Following is my FileRead method in my TextFile class.For some reason, some files end up with an extra three characters at the beginning of the datastream. The characters are not in the filre, and Access 2007 / VBA does not add the characters to all files it reads.

Did I code something incorrectly, or must I leave my hack in the method?

Code:
Public Function FileRead() As Boolean
  On Error GoTo Err_FileRead

  Dim lnFileSize As Long

  'Make sure the read buffer is empty to start with
  strFileReadBuffer = ""

  'Check file size
  lnFileSize = LOF(FileNumber)

  'Read the entire file into memory
  strFileReadBuffer = Input(lnFileSize, FileNumber)

  'Hack to strip off some bazar leading characters from files, needed only sometimes... (shrug)
  If Mid(strFileReadBuffer, 1, 3) = "" Then
    strFileReadBuffer = Mid(strFileReadBuffer, 4)
  End If

  'Good return code
  FileRead = True
  
Exit_FileRead:
  Exit Function

Err_FileRead:
  Call errorhandler_MsgBox("Class: clsObjTxtFileUtils, Subroutine: FileRead()")
  FileRead = False
  Resume Exit_FileRead

End Function
 
Quite interesting little characters. Have you got a sample file we could test on?
 
Google turns up some hits. Seems it's a "Byte Order Mark (BOM) of the Unicode Standard" ref.

Maybe StrConv will work:

Code:
strFileReadBuffer = StrConv(Input(lnFileSize, FileNumber),vbFromUnicode)
Chris
 
Google turns up some hits. Seems it's a "Byte Order Mark (BOM) of the Unicode Standard" ref.

Maybe StrConv will work:

Code:
strFileReadBuffer = StrConv(Input(lnFileSize, FileNumber),vbFromUnicode)
Chris

That definitely did NOT work. That reads files in as hygroscopics!

Good to know the BOM data just has a stray BOM character in it... "who would have thought!" :cool:

I guess I will just leave the code that searchers for a BOM at the beginning of the file for now.

According to:
"Byte order mark"
http://en.wikipedia.org/wiki/Byte_order_mark

"OM use is optional, and, if used, should appear at the start of the text stream."

So hopefully as I have first encountered it, that character should only ever be at Mid(1, 3) of files, so my workaround I think should be adequate.
 
Last edited:
You will be better off performing a Replace() on the stream.
 

Users who are viewing this thread

Back
Top Bottom