VB读写文本文件问题

2024-12-14 07:54:20
推荐回答(3个)
回答1:

'添加窗体Form1,文本框Text1,按钮Command1,Command2,然后添加如下代码:
Private Sub Form_Load()
    Command1.Caption = "读文件"
    Command2.Caption = "写文件"
End Sub

Private Sub Command1_Click() '读文件
    Text1.Text = fileStr("C:\t1.txt")
    MsgBox "读入完毕!"
End Sub

Private Sub Command2_Click() '写文件
    writeToFile "C:\t1.txt", Text1.Text
    MsgBox "写入完毕!"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'功能:根据所给的文件名返回文件的内容
'函数名:fileStr
'入口参数(如下):
'  strFileName 所给的文件名;
'返回值:文件的内容
'备注:sysdzw 于 2007-5-3 提供
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function fileStr(ByVal strFileName As String) As String
    On Error GoTo Err1
    Dim tempInput As String
    Open strFileName For Input As #1
    Do While Not EOF(1)
        Line Input #1, tempInput
        If Right(tempInput, 1) <> Chr(10) Then tempInput = tempInput & Chr(10)
        tempInput = Replace(tempInput, Chr(10), vbCrLf)
        fileStr = fileStr & tempInput
    Loop
    If fileStr <> "" Then fileStr = Left(fileStr, Len(fileStr) - 2)
    Close #1
    Exit Function
Err1:
    MsgBox "不存在该文件或该文件不能访问!", vbExclamation
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'功能:根据所给文件名和内容直接写文件
'函数名:writeToFile
'入口参数(如下):
'  strFileName 所给的文件名;
'  strContent 要输入到上述文件的字符串
'返回值:True或False,成功则返回前者,否则返回后者
'备注:sysdzw 于 2007-5-2 提供
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function writeToFile(ByVal strFileName As String, ByVal strContent As String) As Boolean
    On Error GoTo Err1
    Open strFileName For Output As #1
    Print #1, strContent
    Close #1
    writeToFile = True
    Exit Function
Err1:
    writeToFile = False
End Function

回答2:

楼上的代码我比较怕怕,好长啊!我写的不好,但比较简单.
Private Sub Command1_Click()
Text1.Text = ""
Open ("d:\vb.txt") For Input As #1
Do While Not EOF(1)
Line Input #1, tem
Text1.Text = Text1.Text + tem + vbCrLf
Loop
Close #1
End Sub

Private Sub Command2_Click()
Open "d:\vb.txt" For Output As #1
Print #1, Text1
Close
End Sub

回答3:

'------API------
Private
Declare
Function
GetPrivateProfileString
Lib
"kernel32"
Alias
"GetPrivateProfileStringA"
(ByVal
_
lpApplicationName
As
String,
_
ByVal
lpKeyName
As
Any,
_
ByVal
lpDefault
As
String,
_
ByVal
lpReturnedString
As
String,
_
ByVal
nSize
As
Long,
_
ByVal
lpFileName
As
String)
As
Long
Private
Declare
Function
WritePrivateProfileString
Lib
"kernel32"
_
Alias
"WritePrivateProfileStringA"
(ByVal
_
lpApplicationName
As
String,
_
ByVal
lpKeyName
As
Any,
_
ByVal
lpString
As
Any,
_
ByVal
lpFileName
As
String)
As
Long
'-------读ini文件-------
Public
Function
GetINI(ByVal
Sect
As
String,
ByVal
Key
As
String,
ByVal
INI_Path
As
String)
As
String
Dim
tmp
As
String
*
256
GetPrivateProfileString
Sect,
Key,
1,
tmp,
256,
INI_Path
GetINI
=
tmp
End
Function
'-------读取按钮-------
Private
Sub
Command1_Click()
Dim
FilePath
As
String
FilePath
=
App.Path
&
"\test.ini"
Text1
=
GetINI("m",
"m1",
FilePath)
Text2
=
GetINI("m",
"m2",
FilePath)
Text3
=
GetINI("n",
"m1",
FilePath)
Text4
=
GetINI("n",
"m2",
FilePath)
End
Sub
'-------写入按钮------
Private
Sub
Command2_Click()
Dim
FilePath
As
String
FilePath
=
App.Path
&
"\test.ini"
WritePrivateProfileString
"m",
"m1",
Text1.Text,
FilePath
WritePrivateProfileString
"m",
"m2",
Text2.Text,
FilePath
WritePrivateProfileString
"n",
"m1",
Text3.Text,
FilePath
WritePrivateProfileString
"n",
"m2",
Text4.Text,
FilePath
End
Sub