如何创建txt文件

时间:2014-01-14 08:13:58

标签: vb6

我必须创建一个txt文件,以便在VB6中使用一些大内容。任何人都可以帮我解决这个问题,请告诉我参考文献。

2 个答案:

答案 0 :(得分:17)

有多大?

保持简单:

'1 form with:
'  1 textbox: name=Text1
'  1 command button: name=Command1
Option Explicit

Private Sub Command1_Click()
  Dim intFile As Integer
  Dim strFile As String
  strFile = "c:\temp\file.txt" 'the file you want to save to
  intFile = FreeFile
  Open strFile For Output As #intFile
    Print #intFile, Text1.Text 'the data you want to save
  Close #intFile
End Sub

这将在每次单击命令按钮时替换文件,如果要添加到文件中,则可以打开“for append”而不是“for output”

reference

答案 1 :(得分:17)

这是您在VB6中创建文本文件的方法

Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for writing
Open "C:\Test.txt" For Output As #iFileNo
'please note, if this file already exists it will be overwritten!

'write some example text to the file
Print #iFileNo, "first line of text"
Print #iFileNo, " second line of text"
Print #iFileNo, "" 'blank line
Print #iFileNo, "some more text!"

'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo

Source