Private Sub Command2_Click()
Open "D:\sheji1\script1變數修改.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, strtxt
If InStr(strtxt, "磁芯外半徑") Then
strtxt = Replace(strtxt, "磁芯外半徑", Text1.Text)
End If
s = s & strtxt & vbCrLf
Loop
Close #1
Open "D:\sheji1\script1變數修改2.txt" For Output As #1
Print #1, s;
Close #1
End Sub
代碼如上,用輸入于TEXT1中的數值替換了文本檔案中的“磁芯外半徑“。
但是打開寫入后的文本檔案發現中文地方全部亂碼了,但是如果把磁芯外半徑這個變數改為字符例如cxwbj時,就可以替換成功,這是為什么呢?
uj5u.com熱心網友回復:
這是因為文本檔案的編碼不是Unicode
uj5u.com熱心網友回復:
用二進制方式存取試試....
uj5u.com熱心網友回復:
對啊,Open For Output這種方式打開,再Print輸出的文本,應該是使用本windows系統的默認字符編碼(在簡體中文WINDOWS中一般是GBK)輸出的,看來你這個已經存在的“script1變數修改2.txt”不是用的這個默認編碼。
uj5u.com熱心網友回復:
那我這個代碼該如何修改呢?
uj5u.com熱心網友回復:
我也想知道,我也碰到類似的問題!
uj5u.com熱心網友回復:
給你一個參考書的例子,你自己琢磨一下修改成自己的即可。
Function ReadBinaryText(ByVal bFile As String) As String
''讀文本檔案
On Error GoTo 100
Dim iNum As Integer, v As Variant
iNum = FreeFile()
Open bFile For Binary As #iNum
Get #iNum, 1, v
Close #iNum
ReadBinaryText = v
Exit Function
100:
MsgBox Err.Description
End Function
Sub SaveTextFile(ByVal bFile As String, ByVal bValue As String)
''寫文本檔案
On Error GoTo 100
Dim iNum As Integer, v As Variant, Byt() As Byte
iNum = FreeFile()
Open bFile For Binary As #iNum
Byt = bValue
v = Byt
Put #iNum, 1, v
Close #iNum
Exit Sub
100:
MsgBox Err.Description
End Sub
' API declarations.
Private Const OFS_MAXPATHNAME As Long = 128
'Private Const OF_WRITE As Long = &H1
Private Const OF_READ As Long = &H0
Private Const OF_CREATE As Long = &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 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, lpOverlapped As OVERLAPPED) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function AppPath() As String
AppPath = App.Path
If Right$(AppPath, 1) <> "\" Then
AppPath = AppPath & "\"
End If
End Function
Public Function UnicodeFile_Read_FSO( _
ByVal sFileName As String, _
Optional ByVal TriState As TristateEnum = TristateTrue) As String
Dim objFSO As Object
Dim objStream As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (Not objFSO Is Nothing) Then
Set objStream = objFSO.OpenTextFile( _
sFileName, ForReading, False, TriState)
If (Not objStream Is Nothing) Then
With objStream
UnicodeFile_Read_FSO = .ReadAll
.Close
End With
Set objStream = Nothing
End If
Set objFSO = Nothing
End If
End Function
Public Sub UnicodeFile_Write_FSO( _
ByVal sFileName As String, _
ByVal sText As String, _
Optional ByVal ForWrite As ForWriteEnum = ForWriting, _
Optional ByVal TriState As TristateEnum = TristateTrue)
Dim objFSO As Object
Dim objStream As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (Not objFSO Is Nothing) Then
Set objStream = objFSO.OpenTextFile( _
sFileName, ForWrite, True, TriState)
If (Not objStream Is Nothing) Then
With objStream
.Write sText
.Close
End With
Set objStream = Nothing
End If
Set objFSO = Nothing
End If
End Sub
Public Function UnicodeFile_Read_VB(ByVal sFileName As String, _
Optional ByVal bRemoveBOM As Boolean) As String
Dim FF As Long
Dim b() As Byte
Dim s As String
Const uBOM As String = "?"
On Error Resume Next
FF = FreeFile
Open sFileName For Binary Access Read As FF
ReDim b(LOF(FF))
Get FF, , b
Close FF
s = b
If bRemoveBOM Then
If InStr(s, uBOM) = 1 Then
s = Replace$(s, uBOM, "")
End If
End If
UnicodeFile_Read_VB = s
End Function
Public Sub UnicodeFile_Write_VB(ByVal sFileName As String, _
ByVal sText As String, _
Optional ByVal bInsertBOM As Boolean)
Dim FF As Long
Dim b() As Byte
On Error Resume Next
Kill sFileName
On Error GoTo 0
FF = FreeFile
Open sFileName For Binary Access Write As #FF
If bInsertBOM Then
ReDim b(1)
b(0) = &HFF
b(1) = &HFE
Put #FF, , b
Erase b
End If
b = sText
Put #FF, , b
Close #FF
End Sub
Public Function UnicodeFile_Read_API(ByVal sFileName As String) As String
Dim lpFileInfo As OFSTRUCT
Dim lpOverlapped As OVERLAPPED
Dim szPathName As String * OFS_MAXPATHNAME
Dim hFile As Long
Dim sText As String
Dim lLength As Long
Dim lLengthRet As Long
szPathName = sFileName
With lpFileInfo
.cBytes = Len(lpFileInfo)
.fFixedDisk = 1
.szPathName = szPathName
End With
hFile = OpenFile(sFileName, lpFileInfo, OF_READ)
If (hFile) Then
ReadFile hFile, ByVal StrPtr(sText), lLength, lLengthRet, lpOverlapped
UnicodeFile_Read_API = MidB(sText, 1, lLength)
CloseHandle (hFile)
End If
End Function
Public Function UnicodeFile_Write_API(ByVal sFileName As String, ByVal sText As String) As Boolean
Dim lpFileInfo As OFSTRUCT
Dim lpOverlapped As OVERLAPPED
Dim szPathName As String * OFS_MAXPATHNAME
Dim hFile As Long
Dim lResult As Long
Dim lLengthRet As Long
szPathName = sFileName
With lpFileInfo
.cBytes = Len(lpFileInfo)
.fFixedDisk = 1
.szPathName = szPathName
End With
hFile = OpenFile(sFileName, lpFileInfo, OF_CREATE)
If (hFile) Then
lResult = WriteFile(hFile, ByVal StrPtr(sText), LenB(sText), lLengthRet, lpOverlapped)
UnicodeFile_Write_API = lResult <> 0
CloseHandle (hFile)
End If
End Function
hFile = OpenFile(sFileName, lpFileInfo, OF_CREATE)
If (hFile) Then
lResult = WriteFile(hFile, ByVal StrPtr(sText), LenB(sText), lLengthRet, lpOverlapped)
UnicodeFile_Write_API = lResult <> 0
CloseHandle (hFile)
End If
End Function