Home > Software engineering >  About VB garbled words after the modification of text file contents.
About VB garbled words after the modification of text file contents.

Time:09-24

Private Sub Command2_Click ()
Open "D: \ sheji1 \ script1 variable changes. TXT" For Input As # 1
The Do While Not EOF (1)
The Line Input # 1, STRTXT
If InStr (STRTXT, "outside the core radius") Then
STRTXT=Replace (STRTXT, "outside the core radius, Text1. Text)
End the If
S=s & amp; STRTXT & amp; VbCrLf
Loop
Close # 1
Open "D: \ sheji1 \ script1 variable to modify 2. TXT" For the Output As the # 1
Print # 1, s;
Close # 1
End Sub
Code as above, replace with input to the value in TEXT1 "outside the core radius" of the text file,
But after open the written text files found Chinese place all the code, but if the core outer radius of the variable characters such as CXWBJ instead, can replace the success, is this why?

CodePudding user response:

This is because the code is not Unicode text file

CodePudding user response:

In a binary way access to try...

CodePudding user response:

Yes, Open For Open the Output this way, to Print Output text, use the Windows should be the default character encoding (in simplified Chinese Windows are generally GBK) of the Output, it seems that you of the existing "script1 variable to modify 2. TXT" is not using the default encoding,

CodePudding user response:

That I how to modify the code?

CodePudding user response:

I also want to know, I also encountered similar problems!

CodePudding user response:

Give you a reference example, consider the modified into your own,

 Function ReadBinaryText (ByVal bFile As String) As String 
"' read text files
On Error GoTo 100
Dim iNum As an Integer, v As the Variant
INum=FreeFile ()
The Open bFile For Binary As # iNum
The Get iNum, # 1, v
Close # iNum
ReadBinaryText=v
The Exit Function
100:
MsgBox Err. The Description
End the Function
Sub SaveTextFile (ByVal bFile As String, ByVal bValue As String)
'to write text file
On Error GoTo 100
Dim iNum As an Integer, v As the Variant, Byt () As Byte
INum=FreeFile ()
The Open bFile For Binary As # iNum
Byt=bValue
V=Byt
Put iNum, # 1, v
Close # iNum
The Exit Sub
100:
MsgBox Err. The Description
End Sub


Writing files, if the new content, shorter than the original, the original file spare parts will not be deleted, although does not affect the reading, but affects the size of the file,
So, if you mind this, write files if there are deleted before...

CodePudding user response:

I have a place wrong, VB it save text files are ANSI format, I want to modify the VBS file is utf-8, Chinese characters is in the file, and after the modification, is want to save the original VBS file don't garbled words, is still a utf-8 documents, too sad, find ways to find on the Internet for several days, can read the relevant examples, but change is always up, under the ace to give directions? Best can help me to modify it,

CodePudding user response:

Have to reply

CodePudding user response:

Try:
STRTXT=Replace (STRTXT, "outside the core radius," StrConv (Text1. Text, vbFromUnicode))

CodePudding user response:

refer to 7th floor sinat_26516023 response:
I have a place wrong, VB it save text files are ANSI format, I want to modify the VBS file is utf-8, Chinese characters is in the file, and after the modification, is want to save the original VBS file don't garbled words, is still a utf-8 documents, too sad, find ways to find on the Internet for several days, can read the relevant examples, but change is always up, under the ace to give directions? Best can help me to modify it,

Since utf-8 is the content of the "text", you need to see if its front with 3 bytes of logo,
In utf-8 format output, VB6 to write files in Binary mode, if there is a file header mark, have to handle with,
To replace content, is likely to cause the change of the file size, length increases generally is ok, but if reduce, the end will be superfluous,
So generally output to a new file,

CodePudding user response:

Give you a module:
Option Explicit

'API declarations.
Private Const OFS_MAXPATHNAME As Long=128
'Private Const OF_WRITE As Long=& amp; H1
Private Const OF_READ As Long=& amp; H0
Private Const OF_CREATE As Long=& amp; H1000

Private Const ForReading As Long=1

Public Enum ForWriteEnum
ForWriting=2
ForAppending=8
End Enum

Public Enum TristateEnum
TristateTrue=1 'Opens the file as Unicode
TristateFalse=0 'Opens the file as ASCII
TristateUseDefault=2 '- Use the default system setting
End Enum



Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
Offset As Long
OffsetHigh As Long
HEvent As Long
End Type

Private Type OFSTRUCT
CBytes As Byte
FFixedDisk As Byte
NErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
SzPathName As String * OFS_MAXPATHNAME
End Type

Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead, As Long As lpOverlapped OVERLAPPED) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

The Public Function AppPath () As String
AppPath=App. Path
If Right $(AppPath, 1) & lt;> "" Then
AppPath=AppPath & amp; ""
End the If
End the Function

The Public Function UnicodeFile_Read_FSO (_
ByVal sFileName As String, _
Optional ByVal TriState As TristateEnum=TristateTrue) As String

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related