想读取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))
[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))
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
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
Dim u As Long
Open App.Path & "\setting.txt" For Input As #Im
Line Input #Im, im
Close #Im
u = Val(im)可是我这样是正确的
也不会报错 奇怪了
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))
要是读ini 文件,看13楼我的代码。
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]
关键要看S_speed有没有定义,定义成啥了
然后光这样 Line Input #S_speed, speed是读不出来 S_speed=9这行数据的,
需要一个循环,判断=前面的字符是否是S_speed,这是一行一行的读的
象这种配置文件的操作,
建议参考13楼
可能VB硬转抱在long的0也可以
关键是
'打开后,第一次用 Line Input # 读到的是第一行,怎么会直接跳到第五行了?
显示是读不出想要的数据的
不管有无 Option Explicit
总会报 类型不匹配
Dim i&, speed As Long
i = GetPrivateProfileString("setting", "S_speed", "", strBuff, 13, "X:\Temp\setting.txt")
speed = Left$(strBuff, i)End Sub
Dim i&, Speed As Long
Speed = Left$(strBuff, GetPrivateProfileString("setting", "S_speed", "", strBuff, 13, "X:\Temp\setting.txt"))End Sub文件路径: "X:\Temp\setting.txt" 要按你的实际情况输入。
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
我刚才试了一下:
Dim strTemp$
Open "X:\Temp\setting.txt" For Input As #0
Line Input #0, strTemp
Close
运行,“实时错误 52”:错误的文件名或号码!所以,文件号是不能用 0 的。
[/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 的。
呵呵,晕死~~~~~~都看不明白他到底要读什么? open 语句 # 后面那个值(文件号)和你下面的什么#sp没有什么关系的,这个值可以是255(512?)以内的值,只要不重复就可以了
我就散分了 都辛苦了