文本文件操作的时候,如何简洁地删除某一行啊
本帖最后由 bcrun 于 2011-01-29 08:53:04 编辑 除了二进制貌似没有直接的读取指定行的办法。 不考虑二进制方式? 二进制方式有代码么?学习一下sContent = sContent & TextLine & vbCrLf
就你示例的代码而言,这行是效率低下的根本原因.... 通俗地说所谓文件是连续存储在硬盘上的,没直接删除一行的方法,通常都是读出重写....
二进制方式可以定位,可以重写某一位置的数据...所谓删除就是用空字符串替换指定位置指定长度的字符...但后面的内容不会自动进位,也不能叫删除,改写而已...
iFileName = App.path & "\test.ini "
'先在初始路径
Public Declare Function MWGetConfigInt Lib "kernel32 " Alias "GetPrivateProfileIntA " (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Integer
Public Declare Function GetPrivateProfileString% Lib "kernel32 " Alias "GetPrivateProfileStringA " (ByVal AppName$, ByVal KeyName$, ByVal keydefault$, ByVal ReturnString$, ByVal NumBytes As Integer, ByVal FileName$)
Public Declare Function WritePrivateProfileString% Lib "kernel32 " Alias "WritePrivateProfileStringA " (ByVal AppName$, ByVal KeyName$, ByVal keydefault$, ByVal FileName$)
Dim iFileName As String
'读入整数
Public Function ReadInteger(Section$, Entry$, Default%) As Integer
ReadInteger = MWGetConfigInt(Section$, Entry$, 0, iFileName)
If ReadInteger = 0 And ReadInteger <> Default% Then
WriteInteger Section$, Entry$, Format$(Default%)
ReadInteger = Default%
End If
End Function
'读入长整数
Public Function ReadLong(Section$, Entry$, ByVal Default&) As Long
ReadLong = Val(ReadString(Section$, Entry$, Format(Default&)))
If ReadLong = 0 And ReadLong <> Default& Then
WriteLong Section$, Entry$, Default&
ReadLong = Default&
End If
End Function
'读入字符串
Public Function ReadString(Section$, Entry$, ByVal Default$) As String
Dim mzBuff As String * 255, nChars
Dim Tmp As String
Dim X, Y As Integer
nChars = GetPrivateProfileString(Section$, Entry$, " ", mzBuff, 255, iFileName)
Tmp = IIf(nChars > 0, Left$(mzBuff, nChars), " ")
Y = InStr(Tmp, Chr(0)) - 1
Y = IIf(Y > = 0, Y, 0)
ReadString = IIf(Y > = 1, Left(Tmp, Y), Tmp)
If ReadString = " " Then
WriteString Section$, Entry$, Default$
ReadString = Default$
End If
End Function
'写入整数
Public Sub WriteInteger(Section$, Entry$, ByVal Default&)
WriteString Section$, Entry$, Format$(Default&)
End Sub
'写入整数
Public Sub WriteLong(Section$, Entry$, ByVal Default&)
WriteString Section$, Entry$, Format$(Default&)
End Sub
'写入字符串
Public Sub WriteString(Section$, Entry$, ByVal Default$)
iAPI = WritePrivateProfileString(Section$, Entry$, Default$, iFileName)
End Sub
没试过,一直压箱底来的~~~
来我们一起研究研究~~ 哎!惭愧
这个是读写INI文件的~~ 没有直接删除某一行的办法。
这个是没错滴~~
记得有个APISetFilePointer 在一个文件中设置当前的读位置,然后把文件后半段的内容读到内存
然后,再由APISetFilePointer在一个文件中设置当前的写位置~~~~
反正就是一个字:麻烦
补充:VB , 基础类