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时,就可以替换成功,这是为什么呢?
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时,就可以替换成功,这是为什么呢?
解决方案 »
- vb中如何将日期时间写入数据库中一个文本字段中
- 关于webbrowser的object标签的url、movie、src等问题
- 字符串截取??截取出ab和mn之间的字符和mn之后的字符 在线等你哦
- fpspread控件中如何引用其中的一个单元格,在线等
- 如何实现自动建立数据库中的表?并且在程序中调用
- 高手们帮忙看看,用MSChart的RowLabel 出现的问题!
- 如何设定程序的运行次数?
- MSCOMM地磅通信问题
- 有一个计算,要保留3位小数,第四位非零要向第三位进1,如何做到
- 怎样用vb把本地的bmp,gif文件存到数据库中去
- 写一个创建快捷方式的VB脚本
- 关于VB6获取系统显卡显存容量的问题
''读文本文件
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
写文件时,如果新内容比原来的短,原来文件多余的部分并不会被删除,虽然不影响读取,但是会影响文件的体积,
所以,如果介意这点,写文件之前如果存在则删除.......
strtxt = Replace(strtxt, "磁芯外半径", StrConv(Text1.Text, vbFromUnicode))
要以UTF-8格式输出,VB6中要用Binary模式写文件。如果有文件头标志,得自己处理加上。
要替换内容,很可能会造成文件长度的变化,长度增加一般没事,但如果减少,那文件尾部会出现多余的东西。
因此一般输出到新文件中。
Option Explicit' 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 = &H1000Private Const ForReading As Long = 1Public Enum ForWriteEnum
ForWriting = 2
ForAppending = 8
End EnumPublic Enum TristateEnum
TristateTrue = -1 'Opens the file as Unicode
TristateFalse = 0 'Opens the file as ASCII
TristateUseDefault = -2 'Use default system setting
End EnumPrivate Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End TypePrivate Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName As String * OFS_MAXPATHNAME
End TypePrivate 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 LongPublic Function AppPath() As String
AppPath = App.Path
If Right$(AppPath, 1) <> "\" Then
AppPath = AppPath & "\"
End If
End FunctionPublic 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 FunctionPublic 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 SubPublic 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 FunctionPublic 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 SubPublic 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 lLength = FileLen(sFileName)
sText = String(lLength, " ") 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 FunctionPublic 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