Capl:CSV格式文件解析Demo
文件类别
.csv文件
Demo表格内容
Capl代码
/*@!Encoding:936*/
includes
{
}
variables
{
int64 number;//为BOBAddr服务,无意义
struct FRECUSETTING
{
int ECUIndex[50];
char ECUName[50][50];
byte BOBAddr[50];
int BOBChannel[50];
char Channel[50][50];
char FibexChannel[50][50];
int Termination[50];
int sychronization[50];
}ECUInfr;
}
//此函数使用ECUInfr中的二维数组储存csv表格的所有信息,采用正则表达式的substr_cpy及strstr处理buffer
void CSV_read()
{
dword csvHandle = 0;
char buffer[500];
char buffer1[100][100];
char chrepeat[500]="";
// char *cp; //capl中不能使用指针
int i=0,n=0,m=0,k=1,pos=0,pos1=0;
dword strlength;
dword filemode_txt=0; //文本形式
dword filemode_binary=1;//二进制形式
csvHandle=openFileRead("ECU.csv",filemode_txt);
write("csvHandle is %d",csvHandle);
if(csvHandle!=0)
{
do
{
memcpy(chrepeat,buffer,elcount(chrepeat));
i++;pos=0;pos1=0;n=0; //重置数组下标
fileGetStringSZ(buffer, elcount(buffer), csvHandle); //不推荐使用fileGetString,因为有/n,需要str_replace_regex处理
str_replace(buffer,"\"","");
if(strncmp(chrepeat,buffer,elcount(chrepeat))==0)
{
write("CSV数据读取完毕!");
break;
}
else
{
if(i>1) //不处理第一行标题栏
{
write(buffer);//打印处理得buffer
str_replace(buffer, "\n", "");//如果使用的是dll自动转换得csv文件,需要去除""
strncat(buffer,",",(elcount(buffer)+1)); //给buffer后面添加,",以便做切割
strlength=strlen(buffer);
if(i>2)
{
for(m=0;m<=strlength;m++)
{
pos1=pos;
pos=strstr_off(buffer,pos,","); //搜寻分隔符的位置
substr_cpy(buffer1[n],buffer,pos1,pos-pos1,elcount(buffer1[n]));n++;//开始裁剪并存入buffer1中,注意若使用mbsubstr_cpy_off须保证字符串末尾为/0
if(pos>=(strlength-1)) //canoe:the number of read characters is equal to buffsize -1
{
break;
}
else
{
pos++;
}
}
}
//信息保存,例:调用时采用ECUInfr.ECUName[ECUIndex]
ECUInfr.ECUIndex[k]=_atoi64(buffer1[0]);
memcpy(ECUInfr.ECUName[k],buffer1[1],elcount(ECUInfr.ECUName[k]));
strtoll(buffer1[2],number);
ECUInfr.BOBAddr[k]=number;
ECUInfr.BOBChannel[k]=_atoi64(buffer1[3]);
memcpy(ECUInfr.Channel[k],buffer1[4],elcount(ECUInfr.Channel[k]));
memcpy(ECUInfr.FibexChannel[k],buffer1[5],elcount(ECUInfr.FibexChannel[k]));
ECUInfr.Termination[k]=_atoi64(buffer1[6]);
ECUInfr.sychronization[k]=_atoi64(buffer1[7]);
k++;
}
}
}while(i<=100);
}
//关闭文件
fileClose(csvHandle);
}
testcase CSVTest_Start()
{
CSV_read();
//调用举例
write("ECUIndex is %d ",ECUInfr.ECUIndex[1]); //index为1的ecu地址
write("ECUName is %s ",ECUInfr.ECUName[1]); //index为1的ecu名称
write("BOBAddr is %d ",ECUInfr.BOBAddr[1]); //index为1的ecu的bob地址
write("BOBChannel is %d ",ECUInfr.BOBChannel[1]); //index为1的ecu的bob通道
write("Channel is %s ",ECUInfr.Channel[1]); //index为1的ecu的测试通道名称
write("FibexChannel is %s ",ECUInfr.FibexChannel[1]); //index为1的ecu的fibex定义通道
write("Termination is %d ",ECUInfr.Termination[1]); //index为1的ecu的fibex定义通道
write("sychronization is %d ",ECUInfr.sychronization[1]); //index为1的ecu的fibex定义通道
}
本文来自博客园,作者:{张一默},转载请注明原文链接:https://www.cnblogs.com/YiMo9929/p/17312340.html