' '写TEXT文件(如果文件不存在,则创建该文件) '函数:WritTextFile '参数:FileName 目标文件名.WritStr 写到目标的字符串. '返回值:成功 返回文件内容.失败 返回"" '注:如果同名,目标字符串将覆盖原文件内容. Public Function WritTextFile(Filename As String, WritStr As String) As Boolean '/保存文件 Dim FileID As Long, ConTents As String Dim A As Long, B As Long
On Error Resume Next
FileID = FreeFile Open Filename For Output As #FileID Print #FileID, WritStr Close #FileID WritTextFile = (Err.Number = 0) Err.Clear End Function' '读TEXT文件 '函数:RedTextFile '参数:FileName 打开的TXT文件名. '返回值:成功 返回文件内容.失败 返回"" Public Function RedTextFile(Filename As String) As String Dim FileID As Long Dim InputStr As String Dim LineStr As String
On Error Resume Next
InputStr = "": LineStr = "" FileID = FreeFile() Open Filename For Input As #FileID Do While Not EOF(FileID) ' 循环至文件尾。 LineStr = "" Line Input #FileID, LineStr InputStr = InputStr & LineStr Loop Close #FileID RedTextFile = IIf(Err.Number = 0, InputStr, "") Err.Clear End Function
Private Sub Command1_Click() '读文件 Dim mybyte() As Byte, temp, rate As Double Open "c:\myfile.txt" For Binary As 1 ' suppose your data with "xxx=2" is in file "c:\myfile.txt" ReDim mybyte(LOF(1)) Get #1, , mybyte Close 1 All = StrConv(mybyte, vbUnicode) ' to string temp = Split(All, vbCrLf) temp = Filter(temp, "xxx") rate = CDbl(Split(temp(0), "=")(1)) MsgBox rate End Sub
用line input一行行读到字符串s
if left(s,4)="XXX=" then XXX=val(right(s,len(s)-4)) '假如xxx是数字
循环读取,然后判断
http://www.china-askpro.com/msg44/qa21.shtml还有就是读到richtextbox,然后使用find方法,速度快
'
'写TEXT文件(如果文件不存在,则创建该文件)
'函数:WritTextFile
'参数:FileName 目标文件名.WritStr 写到目标的字符串.
'返回值:成功 返回文件内容.失败 返回""
'注:如果同名,目标字符串将覆盖原文件内容.
Public Function WritTextFile(Filename As String, WritStr As String) As Boolean
'/保存文件
Dim FileID As Long, ConTents As String
Dim A As Long, B As Long
On Error Resume Next
FileID = FreeFile
Open Filename For Output As #FileID
Print #FileID, WritStr
Close #FileID
WritTextFile = (Err.Number = 0)
Err.Clear
End Function'
'读TEXT文件
'函数:RedTextFile
'参数:FileName 打开的TXT文件名.
'返回值:成功 返回文件内容.失败 返回""
Public Function RedTextFile(Filename As String) As String
Dim FileID As Long
Dim InputStr As String
Dim LineStr As String
On Error Resume Next
InputStr = "": LineStr = ""
FileID = FreeFile()
Open Filename For Input As #FileID
Do While Not EOF(FileID) ' 循环至文件尾。
LineStr = ""
Line Input #FileID, LineStr
InputStr = InputStr & LineStr
Loop
Close #FileID
RedTextFile = IIf(Err.Number = 0, InputStr, "")
Err.Clear
End Function
Dim mybyte() As Byte, temp, rate As Double
Open "c:\myfile.txt" For Binary As 1 ' suppose your data with "xxx=2" is in file "c:\myfile.txt"
ReDim mybyte(LOF(1))
Get #1, , mybyte
Close 1
All = StrConv(mybyte, vbUnicode) ' to string
temp = Split(All, vbCrLf)
temp = Filter(temp, "xxx")
rate = CDbl(Split(temp(0), "=")(1))
MsgBox rate
End Sub