VB怎么读取和写入到文本文件中(有规定的格式)
比如一个文本 xxx.txt 内容如下
-------------------
[系统]
登陆名=xxx
密码=yyy
[用户1]
密码=yy1
[用户2]
密码=yy2
[用户3]
密码=yy2
.
.
.
.
[用户n]
密码=yyn
-------------------
我要从数据库中读取出数据然后写到这样的文本中
格式不能变
[解决办法]
INI操作示例
module
Option Explicit
Public Declare Function GetPrivateProfileInt Lib "kernel32 " Alias "GetPrivateProfileIntA " (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32 " Alias "GetPrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32 " Alias "WritePrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Public Declare Function GetWindowsDirectory Lib "kernel32 " Alias "GetWindowsDirectoryA " (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Type SeqNo_1
InputPoint(7) As Byte
DataPointF(19) As Byte
DataPointUnit(4) As Byte
DataPointB(74) As Byte
End Type
Public Type SeqNo_2
TagName(4) As Byte
TagDescrp(29) As Byte
End Type
form 中 2按纽 2文本框
Option Explicit
Private Sub Command1_Click()
'Dim i As Long, s As String
'i = GetPrivateProfileString( "aa ", "c ", " ", s, Len(s), "e:\aa.ini ")
'
'If i > 0 Then
'Text1.Text = s
'End If
' '读取信息
' '读取ABC.INI文件中TIP字段中START的值并打印出来
' '当函数返回值为0时说明读取数据出错
Dim A As Long
Dim T As String * 3
'T = Space(3) ' '事先定义读取值的字串宽度
A = GetPrivateProfileString( "aa ", "e ", " ", T, Len(T), "e:\aa.INI ")
If A = 0 Then MsgBox "找不到所需字段 ": Exit Sub
'Text1.Text = Left$(T, Len(Trim$(T)) - 1)
Text1.Text = T
End Sub
Private Sub Command2_Click()
WritePrivateProfileString "aa ", "c ", "kk ", "e:\aa.ini "
End Sub
Private Sub Form_Load()
End Sub
[解决办法]
读取和写入ini文件的API函数比较方便
[解决办法]
这个最简单了,用API函数太麻烦了,直接从数据库读出来然后按你的格式写入就可以了。
open file1 for output as #1
rs.open "select 用户名,密码 FROM USERS "....
IF RS.RECORDCOUNT> 0 THEN
FOR I=0 TO RS.RECORDCOUNT-1
STR1=RS.FIELDS(“用户名 ").value
print #1,str1
str2=RS.FIELDS(“密码 ").value
print #1,str2
NEXT I
ENDIF
rs.close
close(1)
注:此处用ADO读出数据库的数据,然后再写入的。。。