比如有个文本有很多行aaa=123
bbb=123
ccc=123
...
zzz=123
我想把zzz那一行改成
zzz=456
而其他的都不变,改怎么写程序啊?
我刚开始时想用
print #1,
来跳过不用修改的行,可是结果把哪些行都给擦掉了

解决方案 »

  1.   

    这些东西可以放在ini文件中,那样修改起来就简单了。
      

  2.   

    示例:
    模块:
    Option Explicit
    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 LongPublic Function GetINI(strINIFile As String, strSection As String, _
                strKey As String, strDefault As String)
    On Error GoTo ErrMsg
        Dim strTemp As String
        Dim intLength As Integer
        
        '判断INI文件是否存在
        If Dir(strINIFile) = "" Then
            MsgBox "INI文件“" & strINIFile & "”已被损坏,请联系管理人员或开发人员!", vbExclamation, "警告!"
    '        RepairINIFile strINIFile
            Exit Function
        End If
        strTemp = Space$(256)
        intLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, 255, strINIFile)
        GetINI = Left$(strTemp, intLength)
        Exit Function
    ErrMsg:
        MsgBox "从INI文件“" & strINIFile & "”文件读取数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End FunctionPublic Function WriteINI(strINIFile As String, strSection As String, _
                strKey As String, strValue As String) As Boolean
    On Error GoTo ErrMsg
        Dim n As Integer
        
        WriteINI = False
        'Replace any CR/LF characters with spaces
        If Len(strValue) >= 1 Then
            For n = Len(strValue) To 1
                If Mid$(strValue, n, 1) = vbCr Or Mid(strValue, n, 1) = vbLf Then
                    Mid$(strValue, n, 1) = ""
                End If
            Next n
        End If
        
        n = WritePrivateProfileString(strSection, strKey, strValue, strINIFile)
        WriteINI = True
        Exit Function
    ErrMsg:
        MsgBox "向INI文件“" & gstrCurrPath & strINIFile & "”文件写入数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End Function/////////////////////////////////////////////////////////////////////////
    文件格式
    [Database]
    aaa=123
    bbb=123
    ccc=123
    ...
    zzz=123假设文件名为:
    strFile="E:\My program\DHtj\Config\DSN.INI"
    你上面的需求可以用:
    WriteINI strFile,"Database","zzz","456"
    来实现。
      

  3.   

    哦,放到ini里面那是怎么修改的?请赐教
      

  4.   

    示例:
    模块:
    Option Explicit
    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 LongPublic Function GetINI(strINIFile As String, strSection As String, _
                strKey As String, strDefault As String)
    On Error GoTo ErrMsg
        Dim strTemp As String
        Dim intLength As Integer
        
        '判断INI文件是否存在
        If Dir(strINIFile) = "" Then
            MsgBox "INI文件“" & strINIFile & "”已被损坏,请联系管理人员或开发人员!", vbExclamation, "警告!"
    '        RepairINIFile strINIFile
            Exit Function
        End If
        strTemp = Space$(256)
        intLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, 255, strINIFile)
        GetINI = Left$(strTemp, intLength)
        Exit Function
    ErrMsg:
        MsgBox "从INI文件“" & strINIFile & "”文件读取数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End FunctionPublic Function WriteINI(strINIFile As String, strSection As String, _
                strKey As String, strValue As String) As Boolean
    On Error GoTo ErrMsg
        Dim n As Integer
        
        WriteINI = False
        'Replace any CR/LF characters with spaces
        If Len(strValue) >= 1 Then
            For n = Len(strValue) To 1
                If Mid$(strValue, n, 1) = vbCr Or Mid(strValue, n, 1) = vbLf Then
                    Mid$(strValue, n, 1) = ""
                End If
            Next n
        End If
        
        n = WritePrivateProfileString(strSection, strKey, strValue, strINIFile)
        WriteINI = True
        Exit Function
    ErrMsg:
        MsgBox "向INI文件“" & gstrCurrPath & strINIFile & "”文件写入数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End Function/////////////////////////////////////////////////////////////////////////
    文件格式
    [Database]
    aaa=123
    bbb=123
    ccc=123
    ...
    zzz=123假设文件名为:
    strFile="E:\My program\DHtj\Config\DSN.INI"
    你上面的需求可以用:
    WriteINI strFile,"Database","zzz","456"
    来实现。
      

  5.   

    示例:
    模块:
    Option Explicit
    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 LongPublic Function GetINI(strINIFile As String, strSection As String, _
                strKey As String, strDefault As String)
    On Error GoTo ErrMsg
        Dim strTemp As String
        Dim intLength As Integer
        
        '判断INI文件是否存在
        If Dir(strINIFile) = "" Then
            MsgBox "INI文件“" & strINIFile & "”已被损坏,请联系管理人员或开发人员!", vbExclamation, "警告!"
    '        RepairINIFile strINIFile
            Exit Function
        End If
        strTemp = Space$(256)
        intLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, 255, strINIFile)
        GetINI = Left$(strTemp, intLength)
        Exit Function
    ErrMsg:
        MsgBox "从INI文件“" & strINIFile & "”文件读取数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End FunctionPublic Function WriteINI(strINIFile As String, strSection As String, _
                strKey As String, strValue As String) As Boolean
    On Error GoTo ErrMsg
        Dim n As Integer
        
        WriteINI = False
        'Replace any CR/LF characters with spaces
        If Len(strValue) >= 1 Then
            For n = Len(strValue) To 1
                If Mid$(strValue, n, 1) = vbCr Or Mid(strValue, n, 1) = vbLf Then
                    Mid$(strValue, n, 1) = ""
                End If
            Next n
        End If
        
        n = WritePrivateProfileString(strSection, strKey, strValue, strINIFile)
        WriteINI = True
        Exit Function
    ErrMsg:
        MsgBox "向INI文件“" & gstrCurrPath & strINIFile & "”文件写入数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End Function/////////////////////////////////////////////////////////////////////////
    文件格式
    [Database]
    aaa=123
    bbb=123
    ccc=123
    ...
    zzz=123假设文件名为:
    strFile="E:\My program\DHtj\Config\DSN.INI"
    你上面的需求可以用:
    WriteINI strFile,"Database","zzz","456"
    来实现。
      

  6.   

    就是把这个文件当成INI文件来处理 ,通过API函数实现,示例如上,,,
      

  7.   


    Private Sub Command1_Click()
          Dim FileName As String
          Dim StrArr() As String
          Dim TmpStr() As String
          Dim WritStr As String
          Dim I As Long
          
          
          FileName = "c:\lx1.txt"
          '将文件读到一个字符串数组.
          StrArr = RedTextFile(FileName)
          For I = 0 To UBound(StrArr)
              TmpStr = Split(StrArr, "=")
              '查找符合条件的项
              If UCase$(TmpStr(0)) = "ZZZ" Then
                 '修改该项
                 TmpStr(1) = "456"
                 StrArr(I) = TmpStr(0) & "=" & TmpStr(1)
              End If
              WritStr = WritStr & StrArr(I) & Chr(13)
          Next
          '回写文件
          WritTextFile FileName, WritStr
          
    End Sub
    '
    '读TEXT文件
    '函数:RedTextFile
    '参数:FileName 打开的TXT文件名.
    Public Function RedTextFile(FileName As String) As String()
         Dim FileID As Long
         Dim InputStr As String
         Dim LineStr As String
         Dim RevStr() As String
         Dim ID As Long
         
         On Error Resume Next
         
         InputStr = "": LineStr = ""
         FileID = FreeFile()
         Open FileName For Input As #FileID
              Do While Not EOF(FileID)           ' 循环至文件尾。
                 LineStr = ""
                 ID = ID + 1
                 ReDim Preserve RevStr(ID)
                 Line Input #FileID, LineStr
                 RevStr(ID - 1) = LineStr
              Loop
         Close #FileID
         RedTextFile = RevStr
         Err.Clear
    End Function'
    '写TEXT文件
    '函数:WritTextFile
    '参数:FileName 目标文件名.WritStr 写到目标的字符串.
    '返回值:成功 返回文件内容.失败  返回""
    '注:如果同名,目标字符串将覆盖原文件内容.
    Public Function WritTextFile(FileName As String, WritStr As String) As Boolean
    '/保存文件
        Dim FileID As Long, ConTents As String
        Dim A As Long, B As Long
        
        On Error Resume Next
        
        FileID = FreeFile
        Open FileName For Output As #FileID
             Print #FileID, WritStr
        Close #FileID
        WritTextFile = (Err.Number = 0)
        Err.Clear
    End Function