情况 :
我正在编写关于保存作为传感器值的数据的代码。
如何在一句话中保存两个值,下面的行出错
filehandle:write(" -The Value of the Sensors : "..tostring(valueOfSensor_1)..tostring(valueOfSensor_2).."\n")
我想保存记录
- 传感器值:13 和 20
- 传感器的值:14 和 24
- 传感器的值:16 和 22
- 传感器的值:42 和 12
像这样..有什么建议吗?
我不确定您所说的“出错”是什么意思,但我认为您想要的是:
filehandle:write(" -The Value of the Sensors : "..tostring(valueOfSensor_1).." and "..tostring(valueOfSensor_2).."\n")
即你所做的只是将两个数字作为字符串放在一起,你想要的是在它们之间插入字符串“和”。
你也可以做
filehandle:write(" -The Value of the Sensors : ", valueOfSensor_1, " and ", valueOfSensor_2, "\n" )
因为 write 将自动将所有参数转换为字符串并将它们连接起来,就像您使用 .. 操作一样。
唯一需要检查的是您以“w”(写入)作为模式而不是“r”(读取)打开文件,并且打开工作正常(文件句柄不为零)。
最好的方法(恕我直言)是使用string.format
local MyString = "-The value of the Sensors : %d and %d\n"
filehandle:write( MyString:format(valueOfSensor_1, valueOfSensor_2) )