想读取setting.txt里的S_speed的数据9:
[setting]
lp=69
mp=67
La=28
Im=58
S_speed=9
W=120
X=180
Y=140
Z=200为什么读不出来,Open App.Path & "\setting.txt" For Input As #S_speed这行报错!
Open App.Path & "\setting.txt" For Input As #S_speed
          Line Input #S_speed, speed
Close #S_speed
          result& = MMC_sendCommand("M" & "N" & "," & "S" & "V" & Round(speed * 145636))

解决方案 »

  1.   

    是读取ini还是读txt文件,代码不全,不知道#S_speed是什么,这里#后应跟数字
      

  2.   

    读取ini文件,就是把S_speed为9的数字读取出来
      

  3.   

    Private Sub Command1_Click()
        Dim S_speed As Integer
        Dim speed As String, S_speed_value As Integer
        
        S_speed = FreeFile
        Open App.Path & "\setting.txt" For Input As #S_speed
        Do While Not EOF(S_speed)
            speed = ""
            Line Input #S_speed, speed
            If InStr(speed, "S_speed=") Then '注意看你的 S_speed= 中间有没有空格
                S_speed_value = Trim(Mid(speed, InStr(speed, "S_speed=")))
                Exit Do
            End If
        Loop
        
        Close #S_speed
        result& = MMC_sendCommand("M" & "N" & "," & "S" & "V" & Round(S_speed_value * 145636))
    End Sub
      

  4.   

    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
      

  5.   

    是ini  刚才写错了 以为是txt了 把S_speed值记为speed:Line Input #S_speed, speed
      

  6.   

    king06你的S_speed_value是做什么用呢
      

  7.   

    S_speed_value 是取到的值既然是ini,就用6楼的API吧
      

  8.   

    给你个例子
    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 LongPrivate Sub Command1_Click()Dim strFile As String
        Dim strSection As String
        Dim strKey As String
        Dim strValue As String
        Dim lng As Long
        
        strFile = "C:\Temp\1.txt"
        strSection = "Setting"
        strKey = "S_speed"
        
        strValue = Space$(80)
        
        lng = GetPrivateProfileString(strSection, strKey, "", strValue, 80, strFile)
        
        strValue = Left$(strValue, lng)
        
        MsgBox strValue
    End Sub
      

  9.   

        Dim Im As String
        Dim u As Long
        Open App.Path & "\setting.txt" For Input As #Im
            Line Input #Im, im
        Close #Im
        u = Val(im)可是我这样是正确的
      

  10.   

     这样Open App.Path & "\setting.txt" For Input As #Im
    也不会报错  奇怪了
      

  11.   

    debug.print s_speed  '看是什么
    Open App.Path & "\setting.txt" For Input As #S_speed
              Line Input #S_speed, speed
    Close #S_speed
              result& = MMC_sendCommand("M" & "N" & "," & "S" & "V" & Round(speed * 145636))
      

  12.   

    楼主的问题到底是什么?
    要是读ini 文件,看13楼我的代码。
      

  13.   

    debug.print s_speed  什么也没有看见
      

  14.   

    [code=VB]Dim Im As String
    Dim u As Long
    Open "x:\temp\setting.txt" For Input As #Im     '此处 Im 还没有赋值,不出错?
    Line Input #Im, Im          '前面要文件号,后面要字符串
    Close #Im
    u = Val(Im)     '打开后,第一次用 Line Input # 读到的是第一行,怎么会直接跳到第五行了?/code]
      

  15.   

    楼主的问题貌似没有问题
    关键要看S_speed有没有定义,定义成啥了
    然后光这样 Line Input #S_speed, speed是读不出来 S_speed=9这行数据的,
    需要一个循环,判断=前面的字符是否是S_speed,这是一行一行的读的
    象这种配置文件的操作,
    建议参考13楼
      

  16.   

    '此处 Im 还没有赋值,不出错?
    可能VB硬转抱在long的0也可以
    关键是
    '打开后,第一次用 Line Input # 读到的是第一行,怎么会直接跳到第五行了?
    显示是读不出想要的数据的
      

  17.   

    好象是的
    不管有无 Option Explicit
    总会报  类型不匹配
      

  18.   

    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 LongPrivate Sub Command1_Click()    Dim strBuff As String * 16
        Dim i&, speed As Long
        i = GetPrivateProfileString("setting", "S_speed", "", strBuff, 13, "X:\Temp\setting.txt")
        speed = Left$(strBuff, i)End Sub
      

  19.   

    Option ExplicitPrivate 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 LongPrivate Sub Command1_Click()    Dim strBuff As String * 16
        Dim i&, Speed As Long
        Speed = Left$(strBuff, GetPrivateProfileString("setting", "S_speed", "", strBuff, 13, "X:\Temp\setting.txt"))End Sub文件路径: "X:\Temp\setting.txt"  要按你的实际情况输入。
      

  20.   

    Private Sub Command1_Click()
        Dim S_speed As Integer
        Dim speed As String, S_speed_value As Integer
        
        S_speed = FreeFile
        Open App.Path & "\setting.txt" For Input As #S_speed
        Do While Not EOF(S_speed)
            speed = ""
            Line Input #S_speed, speed
            If InStr(speed, "S_speed=") Then '注意看你的 S_speed= 中间有没有空格
                S_speed_value = Trim(Mid(speed, InStr(speed, "S_speed=")))
                Exit Do
            End If
        Loop
        
        Close #S_speed
        result& = MMC_sendCommand("M" & "N" & "," & "S" & "V" & Round(S_speed_value * 145636))
    End Sub
      

  21.   

    美羊羊,vbNullChar 转换成 Long ,也会出错的!它不会被自动转换成 0 。
    我刚才试了一下:
    Dim strTemp$
    Open "X:\Temp\setting.txt" For Input As #0
    Line Input #0, strTemp
    Close
    运行,“实时错误 52”:错误的文件名或号码!所以,文件号是不能用 0 的。
      

  22.   

    谢谢老大指点
    [/Quote]
    [Quote=引用 31 楼 chen8013 的回复:]
    引用 25 楼 sdfkfkd 的回复:
     '此处 Im 还没有赋值,不出错?
     可能VB硬转抱在long的0也可以
     ..............
     美羊羊,vbNullChar 转换成 Long ,也会出错的!它不会被自动转换成 0 。
     我刚才试了一下:
    VB codeDim strTemp$
    Open"X:\Temp\setting.txt"For InputAs #0
    Line Input #0, strTemp
    Close
     运行,“实时错误 52”:错误的文件名或号码! 所以,文件号是不能用 0 的。
      

  23.   


     呵呵,晕死~~~~~~都看不明白他到底要读什么? open 语句 # 后面那个值(文件号)和你下面的什么#sp没有什么关系的,这个值可以是255(512?)以内的值,只要不重复就可以了 
      

  24.   

    LZ想要让人帮忙,又不帖全你的code这样很难
      

  25.   

    确实如6楼所说 调用API 解决了 还感谢尹兄!
    我就散分了 都辛苦了