在VB中如何新建/打开一个文本文件?

china_people 2003-12-29 03:15:25
在VB中如何新建/打开一个文本文件?
...全文
474 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cenyfly 2003-12-29
  • 打赏
  • 举报
回复
创建一个文本文件,这可以通过两种方法完成。一种方法是使用FileSystemObject对象的 CreateTextFile 方法。要创建一个空文本文件,可以用以下语句:

Dim fsoTest As New FileSystemObject, fil1 As File
Set fil 1= fsoTest.CreateTextFile(“c:\testfile.txt", True)

  第二种方法是使用 FileSystemObject 对象带 ForWriting 标志设置的 OpenTextFile 方法。

Dim fsoTest As New FileSystemObject, ts1 As New TextStream
Set ts1 = fsoTest.OpenTextFile(“c:\testfile.txt", ForWriting)
leolan 2003-12-29
  • 打赏
  • 举报
回复
Open 陳述式


讓我們擁有對檔案做輸入/輸出(I/O)的能力。

語法

Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

Open 陳述式的語法具有以下幾個單元:

單元 說明
pathname 必要的引數。它是個字串運算式,用來指定檔名包含有目錄及磁碟機名稱。
mode 必要的引數。它是個關鍵字,用來指定檔案模式:有 Append、Binary、Input、Output、或 Random 等。如果沒有指定則以 Random 模式開啟檔案。
access 選擇性引數。它是個關鍵字,用來指定所開啟的檔案可被允許的動作:有 Read、Write、或 Read Write 等。
lock 選擇性引數。它是個關鍵字,用來指出其它處理程序對於本開啟的檔案可被限制的動作:有 Shared、Lock Read、Lock Write、和 Lock Read Write 等。
filenumber 必要的引數。一個正確的檔案代碼,範圍從 1 到 511。可以使用 FreeFile 函數去取得下一個可用的檔案代碼。
reclength 選擇性引數。它必須是小於或等於 32,767(位元組)的一個數字。 對於隨機存取檔案而言,這個數值就是記錄的長度。對於循序檔案而言,這個數值就是暫存區的字元數。


請注意

您在對檔案做任何 I/O 動作之前,必須先開啟它。Open 陳述式會配置一個暫存區以供檔案做 I/O 時使用,並且會決定此暫存區所使用的存取模式。

如果由引數 pathname 所指定的檔案不存在,但是其指定的檔案開啟模式為 Append、Binary、Output、或 Random 的話,Open 陳述式仍會依照此檔名產生一個檔案。

如果所指定的檔案已由其它程序所開啟,且所指定的存取型態是不被允許的話,則 Open 動作會失敗,而且會有錯誤發生。

如果引數 mode 是 Binary 模式,則 Len 子句會被忽略掉。

重要 在 Binary、Input 和 Random 等模式下,您可以使用不同的檔案代碼去開啟同一個檔案,而不必先將檔案關閉。但在 Append 和 Output 模式下,若要以不同的檔案代碼去開啟同一個檔案,則您必須在開啟前先關閉該檔案。
leolan 2003-12-29
  • 打赏
  • 举报
回复
Option Explicit
Private Const MC_MSG = "You have clicked one button!Thank you and good luck~"
Private Const MC_FILENAME = "TLogErr.txt"

Private Function MsgBox(ByVal txtMsg As String, Optional _ ByVal intButtons As Integer, Optional ByVal txtTitle As String) _ As Integer
On Error GoTo ErrHandle
Dim l_intResult As Integer
l_intResult = VBA.Interaction.MsgBox(txtMsg, intButtons, txtTitle)
If Not IsMissing(intButtons) Then
If (intButtons And vbCritical) = vbCritical Then
Call logError(txtMsg, txtTitle, CStr(Now))
End If
End If
MsgBox = l_intResult
Exit Function
ErrHandle:
Err.Raise Err.Number
End Function

Private Sub Command1_Click()
Call MsgBox(MC_MSG, vbOKOnly + vbCritical, App.EXEName & "[" _ & App.Major & "." & App.Minor & "." & App.Revision & "]")
End Sub

Private Function logError(ByVal strMsg As String, Optional _ ByVal strTitle As String, Optional ByVal strTime As String)
On Error GoTo ErrHandle
Dim l_strPath As String
Dim FileNum As Long
FileNum = FreeFile
l_strPath = IIf(StrComp(Right(App.Path, 1), "\", vbTextCompare) _ = 0, App.Path, App.Path & "\")
Open l_strPath & MC_FILENAME For Append As #FileNum
If LOF(FileNum) = 0 Then
Print #FileNum, "---------------------------錯誤信息--------------------------- ------標題------ ---------時間---------"
End If
Print #FileNum,
Print #FileNum, strMsg & Space(65 - LenB(StrConv(strMsg, vbFromUnicode))), strTitle & Space(15 - LenB(StrConv(strTitle, vbFromUnicode))), strTime
Close #FileNum
Exit Function
ErrHandle:
Close #FileNum
Err.Raise Err.Number
End Function

Private Sub Command2_Click()
Unload Me
End Sub
jhzhou882 2003-12-29
  • 打赏
  • 举报
回复
引入MiroSoft Scripting runtime

Private uScpFilsysobj As New Scripting.FileSystemObject
Private uScpTextStream As Scripting.TextStream


Set uScpTextStream = uScpFilsysobj.CreateTextFile("c:\aaa.txt")'建

Set uScpFilefle = uScpFilsysobj.GetFile("c:\aaa.txt") '如果存在这样打开
Set uScpTextStream = uScpFilefle.OpenAsTextStream(ForAppending)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧