Use VB to process the import of fixed length text

zhaozj2021-02-16  38

Writing a text using a splitter text into a database that may be the most often used, but the text imported is not as simple as the former (personal view). Because there is a need to deal with a technical problem, all characters in VB are operated in Unicode, so the English characters, numbers, and two bytes of Chinese characters are two bytes in VB. So after I read a line of text Readline, if you are a string mixed from a single digit by a single-double byte, then there is no way to use the MID, the Left such function determines the length of this string. Therefore, I have never found a good way to deal with, so I have been depressed for a long time.

Finally, I can only use the API to process, calling the copyMemory to string Copy, because this function can be operated by byte. The effect is still possible in use, and I have a layer of function in the following.

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' API declaration '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' Private declare sub copymemory lib "kernel32" Alias ​​"RTLmoveMemory" (PDS AS ANY, PSRC As Any, Byval Bytelen As Long)

'Function returns a string of bytes Private Function GetByteString (ByVal strInput As String, ByVal intLen As Integer) As String Dim strRtn As String strRtn = Space $ (intLen) Call CopyMemory (ByVal strRtn, ByVal strInput, intLen) GetByteString = strRtnEnd Function

'The following is a read file processing and splitting string Dim fsoReadFile As FileSystemObject' FSO Object Dim flFile As File 'File Object Dim tsTxt As TextStream' open a text file Set fsoReadFile = New FileSystemObject Set flFile = fsoReadFile.GetFile (strFileName) Set tsTxt = flFile.OpenAsTextStream (ForReading) Do While Not tsTxt.AtEndOfStream strTMP = tsTxt.ReadLine 'reads the first ten bytes of character strInsert (0) = GetByteString (strTMP, 10)' original string shortened strTMP = Right ( StrtMP, LEN (STRTMP) - LEN (Strinsert (0))) · · · · · · · · The length is intercepted by the length, so that loop tstxt.close set fsoreadfile = Nothing This is what I can think of, hope Friends who have a good way to correct (40star@163.com)!

转载请注明原文地址:https://www.9cbs.com/read-27493.html

New Post(0)