如何使用CAPL读取csv中的浮点数? [英] How to read a float in a csv with CAPL?
问题描述
我有一个.csv文件,我想读取其格式的数据,而不是字符串格式的数据。
如果可以的话,这是将文件保存在读取缓冲区中的函数。
fileGetString(readbuffer,elcount(readbuffer ),readHandle)!= 0)
我的数据格式如下:
温度; 12.25; 15.65; -25.12; 80;
时间; 1; 2; 4; 7;
我想将温度保存在缓冲区 Temperature [i]
并与时间 Time [i]
一样吗?如何在CAPL中做到这一点? p>
我知道我可以读取String之类的每个数据,并进行一些操作将其转换为整数或浮点数,但是我想优化代码并读取其格式的每个数据。
您可以使用 strtod()
将字符串转换为浮点数。
只是为了好玩,下面是完整的示例:
开始
{
dword fh;
char text [255];
double temperature [4],Time [4];
int i;
/ *打开文件* /
fh = openFileRead( a.csv,0);
if(!fh){
write(错误:打开文件失败!);
的回报;
}
/ *读取温度行* /
if(!fileGetString(text,elcount(text),fh)||
strstr(text ,温度;)!= 0){
write(错误:错误的文件格式,未找到'温度'!);
的回报;
}
/ *获取温度值* /
getValuesFromCsvString(text,Temperature);
/ *读取时间行* /
if(!fileGetString(text,elcount(text),fh)||
strstr(text, Time; )!= 0){
write(错误:文件格式错误,找不到时间!);
的回报;
}
/ *获取时间值* /
getValuesFromCsvString(text,Time);
/ *输出值* /
for(i = 0; i< elcount(Temperature); i ++)
write( Temperature [%i] =%6.2f ,i,温度[i]);
for(i = 0; i write( Time [%i] =%2.0f,i,Time [i]);
}
int getValuesFromCsvString(char text [],double vals [])
{
long i,pos;
双重保留;
pos = strstr(text,;);
str_replace(text,;,);
for(i = 0; i< elcount(vals); i ++){
pos = strtod(text,pos,res);
if(pos> = 0)
vals [i] = res;
休息
;
}
返回0;
}
输出:
温度[0] = 12.25
温度[1] = 15.65
温度[2] = -25.12
温度[3] = 80.00
时间[0] = 1
时间[1] = 2
时间[2] = 4
时间[3] = 7
I have a .csv file and I want to read the data in its format, not at string. This is the function that save the file in a readbuffer if is ok.
fileGetString(readbuffer,elcount(readbuffer),readHandle)!=0)
And I have the data is in this format:
Temperature;12.25;15.65;-25.12;80;
Time;1;2;4;7;
I want save the temperature in a buffer "Temperature[i]
"
and do the same with the time "Time[i]
"
How can I do this in CAPL?
I know that I can read each data like String and cast to integer or float doing some operations, but I want optimize code and read each data in its format.
You can convert an string to an float number using strtod()
.
Just for fun, here is complete example:
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;
}
Output:
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
这篇关于如何使用CAPL读取csv中的浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!