大家好,我遇到这样一个问题,我能写一个文件,为什么不能读,代码如下,请各位兄弟帮我看一下。 Dim str As String
Set fs = CreateObject("Scripting.FileSystemObject") '建立一个文件系统
Set a = fs.CreateTextFile(App.Path & "\backup\std_code.txt", True)
mysql = "select * from std_code"
If Not sqlReset(mysql) Then
Exit Sub
End If
a.WriteLine (myReset(0) & "," & myReset(1) & "," & myReset (2)) '写入文件库以.号作为一行的结束
myReset.MoveNext
Wend
a.Close 但是,我为什么不能读呢?
dim str as String
str= a.readline()
然后,怎么知道是读一个文件的最后了呢?
求解
Set fs = CreateObject("Scripting.FileSystemObject") '建立一个文件系统
Set a = fs.CreateTextFile(App.Path & "\backup\std_code.txt", True)
mysql = "select * from std_code"
If Not sqlReset(mysql) Then
Exit Sub
End If
a.WriteLine (myReset(0) & "," & myReset(1) & "," & myReset (2)) '写入文件库以.号作为一行的结束
myReset.MoveNext
Wend
a.Close 但是,我为什么不能读呢?
dim str as String
str= a.readline()
然后,怎么知道是读一个文件的最后了呢?
求解
Object 必需的。始终是一个 FileSystemObject 的名字。
filename 必需的。字符串表达式,它标识了打开的文件。
iomode 可选的。表示输入/输出方式。可为两个常数之一:ForReading或 ForAppending。
create 可选的。Boolean 值,它表示如果指定的 filename 不存在是否可以创建一个新文件。如果创建新文件,其值为 True。若不创建文件其值为 False。缺省值为 False。
format 可选的。三种 Tristate 值之一,用于指示打开文件的格式。如果省略,则文件以 ASCII 格式打开。
设置值iomode 参数可为下面设置值的任何一个:常数 值 描述
ForReading 1 打开一个只读文件。不能对此文件进行写操作。
ForAppending 8 打开一个文件并写到文件的尾部。
Format 参数可为下面设置值的任何值:常数 值 描述
TristateUseDefault -2 使用系统缺省打开文件。
TristateTrue -1 以 Unicode 格式打开文件。
TristateFalse 0 以 ASCII 格式打开文件。
说明下面的代码举例说明了使用 OpenTextFile 方法打开一个用于追加文本的文件:Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
Dim fso, MyFile, thefile, IniFile, retstring
If Right(App.Path, 1) = "\" Then
IniFile = App.Path & "MailChecker.INI"
Else
IniFile = App.Path & "\MailChecker.INI"
End If Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(IniFile) = False Then
GetParameter = 1
Exit Function
End If
Set thefile = fso.OpenTextFile(IniFile, 1, False)
Do While thefile.AtEndOfStream <> True
retstring = thefile.ReadLine
If InStr(retstring, "ServerName = ") <> 0 Then
str_ServerName = Right(retstring, Len(retstring) - Len("ServerName = "))
End If
If InStr(retstring, "LoginID = ") <> 0 Then
str_LoginID = Right(retstring, Len(retstring) - Len("LoginID = "))
End If
If InStr(retstring, "Password = ") <> 0 Then
str_Password = Right(retstring, Len(retstring) - Len("Password = "))
End If
If InStr(retstring, "Interval = ") <> 0 Then
lng_Interval = Right(retstring, Len(retstring) - Len("Interval = "))
End If
If InStr(retstring, "Timeout = ") <> 0 Then
lng_Timeout = Right(retstring, Len(retstring) - Len("Timeout = "))
End If
If InStr(retstring, "Icon = ") <> 0 Then
lng_IconIndex = Right(retstring, Len(retstring) - Len("Icon = "))
End If
Loop
thefile.Close
GetParameter = 0
End FunctionPublic Function LetParameter()
Dim fso, MyFile, IniFile
If Right(App.Path, 1) = "\" Then
IniFile = App.Path & "MailChecker.INI"
Else
IniFile = App.Path & "\MailChecker.INI"
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(IniFile, True)
MyFile.writeline "/* DO NOT REMOVE UNDER LINES */"
MyFile.writeline
MyFile.writeline "[Mail Checker Settings]"
MyFile.writeline "ServerName = " & str_ServerName
MyFile.writeline "LoginID = " & str_LoginID
MyFile.writeline "Password = " & str_Password
MyFile.writeline "Interval = " & lng_Interval
MyFile.writeline "Timeout = " & lng_Timeout
MyFile.writeline "Icon = " & lng_IconIndex
MyFile.Close
Set fso = Nothing
End Function
Private Sub CmdRead_Click()
Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream, s As String
Set file1 = fsoTest.GetFile(“C:\testfile.txt")
Set ts = file1.OpenAsTextStream(ForReading)
'读取一行
s = ts.ReadLine
MsgBox s
ts.Close
End Sub
Private Sub CmdWrite_Click()
Dim txtfile As File, ts As TextStream
Set txtfile = fsoTest.GetFile(“c:\testfile.txt")
Set ts = txtfile.OpenAsTextStream(ForWriting)
'使用Write方法写入一行。
ts.Write (“This is only a Test")
' 写入一行带有换行符的文本。
ts.WriteLine (“Testing 1, 2, 3.")
' 向文件中写入三个换行符。
ts.WriteBlankLines (3)
ts.Close
End Sub
Conn.BeginTrans
FileNumber = FreeFile
Open FileName For Input As #FileNumber
Do While Not EOF(FileNumber)
Line Input #FileNumber, S
If S <> "" Then
's就是你读入的每一条数据,你可以通过sql语句进行操作了。
'
Else
'On Error Resume Next
End If
Loop
Close FileNumber
Conn.CommitTrans
Dim tFileNumberRead As Integer
Dim tFileNumberWrite As Integer
Dim tStrRead As String
Dim tStrWrite As StringpFileName=App.Path & "\backup\Test.txt"tFileNumberRead=FreeFile
Open pFileName For Input As #tFileNumberRead '以第一个文件号打开作为读。tFileNumberWrite=FreeFile
Open pFileName For Input As #tFileNumberWrite '以第二个文件号打开作为写。'写文件
Print #tFileNumberWrite,tStrWrite'读文件
Do
Line Input #tFileNumberRead, tStrRead
Loop Until EOF(tFileNumberRead)Close #tFileNumberReadClose #tFileNumberWrite