请教读取ini文件的api函数的使用问题
以下是一个读取ini文件数据的函数,有几个问题请教一下
1.我的变量 strtmp 应该预留多少的字符呀,是应该全部填充空格呀,还是用 ascii码 0 来填充呢?,
关键是预留多少个字符,有没有规定,还是随便设?
2.如果用事先填充的是 null,即ascii码的0,是不是就不需要截尾了.因为这个函数也是网上找的,所以一些地方不明白.当然了,这个函数需要事先声明API函数,我这里省略了.
Public Function GetIniKey(strSection As String, strKey As String) As String    On Error GoTo errhandle
    Dim strtmp  As String
    Dim lngRet  As String
    Dim i       As Integer
    Dim strTmp2 As String
    
    '先将strtmp定义成1024个字符的长度,保证一定能装下返回的字串
    'strtmp = String$(1024, Chr(32))
    strtmp = String$(1024, 0)    lngRet = GetPrivateProfileString(strSection, strKey, "", strtmp, Len(strtmp), strINI)
    
    'strtmp现在已经是返回的字串了,所以要进行截尾处理
    strtmp = Trim(strtmp)
    'strTmp2 = ""
    
    'ascii码为0对应的为空字符
'    For i = 1 To Len(strtmp)
'        If Asc(Mid(strtmp, i, 1)) <> 0 Then
'            strTmp2 = strTmp2 + Mid(strtmp, i, 1)
'        End If
'    Next i
    
    GetIniKey = strtmp
    GetIniKey = strtmp2
    Exit Function
errhandle:
    GetIniKey = ""End Function

解决方案 »

  1.   

    1)由你 ini 的内容决定缓冲区的大小。如何填充对调用没影响,函数只关心参数中的缓存区长度。
    2)返回字符串以 \0 字符结尾,必须截尾。如果你不关心 ini 值末尾是否有空格,如下:
    strtmp = Trim(Replace(strtmp, Chr(0), " "))
      

  2.   

    如果进行截尾如何实现好呢?下面的方式行吗?有更好的实现方法吗?
       'ascii码为0对应的为空字符 
    '    For i = 1 To Len(strtmp) 
    '        If Asc(Mid(strtmp, i, 1)) <> 0 Then 
    '            strTmp2 = strTmp2 + Mid(strtmp, i, 1) 
    '        End If 
    '    Next i
      

  3.   

    我不明白的是,trim()本身不就是截尾吗?难道API还有特珠之处。
    以前听说过,对于字符串,VB的处理与C语言不一样,所以涉及到字符串结尾标记的问题。
    C是以\0做为结束标记,可是VB不是这样
      

  4.   


    1 无论预先赋值空格还是 Null,实际目的是分配存储空间。只要长度大于等于预期取回的字符串长度即可。如果预设短了,则会取回不完整的字符串。2 函数的作者没有正确理解 API 函数。实际上,不存在他所理解的截尾问题。返回值就是取回的字符数。lngRet = GetPrivateProfileString(strSection, strKey, "", strtmp, Len(strtmp), strINI)strtmp = Left(strtmp, lngRet)
      

  5.   

    C 的字符串长度和 VB 的字符串长度对中文是不一致的。4 楼的 Left 操作无法正确截尾。
      

  6.   

    http://www.m5home.com/bbs/dispbbs.asp?boardid=28&Id=452&page=2送你个封装好的.
      

  7.   


    下面第一个函数也是网上找的,第二个是您的函数,
    可是我看到第一种写法简洁,没有用循环,可是您的代码用了循环,
    我想问,第一种写法对吗?您为何要用循环来处理截尾呢?请再指教。Public Function GetIniKey(strSection As String, strKey As String) As String
        On Error GoTo errhandle
        Dim strResult  As String * 255          '固定255长度
        Dim lngRet  As Long
        
        lngRet = GetPrivateProfileString(strSection, strKey, "", strResult, Len(strResult), strINI)
        GetIniKey = Left(strResult, InStr(strResult, Chr(0)) - 1)    Exit Function
    errhandle:
        GetIniKey = ""
    End FunctionFunction GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
        Dim ResultString As String * 144, Temp As Integer
        Dim S As String, I As Integer    Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, IniFileName)    If Temp% > 0 Then
            S = ""
            For I = 1 To 144
                If Asc(Mid$(ResultString, I, 1)) = 0 Then
                    Exit For
                Else
                    S = S & Mid$(ResultString, I, 1)
                End If
            Next
        Else
            Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, IniFileName)
            S = DefString
        End If
        GetIniS = S
    End Function
      

  8.   

    写了个读取ini文件的函数,同时能去掉用Tab键分隔的注释信息Public Function GetStrVal(ByVal strFile As String, ByVal strSection As String, ByVal strKeyName As String, Optional ByVal strDefault = "") As String
        Dim l As Long, i As Long, j As Long
        Dim sReturn As String
        On Error Resume Next    sReturn = Space(200)
        If InStr(strFile, "\") = 0 Then
            strFile = App.Path & "\" & strFile
        End If
        l = GetPrivateProfileString(strSection, strKeyName, strDefault, sReturn, 200, strFile)
        If l > 0 Then
            sReturn = Trim(StrConv(LeftB(StrConv(sReturn, vbFromUnicode), l), vbUnicode))
            j = InStr(sReturn, vbTab)
            If i > 0 Then
                If j > 0 And j < i Then
                    l = j - 1
                Else
                    l = i - 1
                End If
            ElseIf j > 0 Then
                l = j - 1
            End If
            If l > 0 Then
                sReturn = Trim(Left(sReturn, l))
            End If
            GetStrVal = sReturn
        End If
    End Function
      

  9.   

    HIA.代码不是我写的.....不过用着没啥问题,就没有去管了,哈哈.