您可以使用 将字符串转换为浮点数strtod()
。只是为了好玩,这里是完整的例子:
on start
{
dword fh;
char text[255];
double Temperature[4], Time[4];
int i;
/* open file */
fh = openFileRead("a.csv",0);
if (!fh) {
write ("ERROR: Open file failed!");
return;
}
/* read the 'Temperature' line */
if (!fileGetString(text, elcount(text), fh) ||
strstr(text, "Temperature;") != 0) {
write("ERROR: Wrong file format, 'Temperature' not found!");
return;
}
/* get the 'Temperature' values */
getValuesFromCsvString(text, Temperature);
/* read the 'Time' line */
if (!fileGetString(text, elcount(text), fh) ||
strstr(text, "Time;") != 0) {
write("ERROR: Wrong file format, 'Time' not found!");
return;
}
/* get the 'Time' values */
getValuesFromCsvString(text, Time);
/* output values */
for (i = 0; i < elcount(Temperature); i++)
write("Temperature[%i] = %6.2f", i, Temperature[i]);
for (i = 0; i < elcount(Time); i++)
write("Time[%i] = %2.0f", i, Time[i]);
}
int getValuesFromCsvString(char text[], double vals[])
{
long i, pos;
double res;
pos = strstr(text, ";");
str_replace(text, ";", " ");
for (i = 0; i < elcount(vals) ; i++) {
pos = strtod(text, pos, res);
if (pos >= 0)
vals[i] = res;
else
break;
}
return 0;
}
输出:
Temperature[0] = 12.25
Temperature[1] = 15.65
Temperature[2] = -25.12
Temperature[3] = 80.00
Time[0] = 1
Time[1] = 2
Time[2] = 4
Time[3] = 7