当前位置:网站首页>CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
2022-07-06 09:04:00 【蚂蚁小兵】
相关文章

前言
- CAPL脚本中关于相对路径,绝对路径操作的几个傻傻分不清的内置函数
- 演示软硬件环境 Win10 x64 ; CANoe 11 SP2 x64


getAbsFilePath
- .
long getAbsFilePath(char relPath[], char absPath[], long absPathLen) - 根据下面
help文档的解释,就是根据输入的相对路径,输出绝对路径。 - 那么问题来了,输出的绝对路径 ,根目录是什么呢?
- 如果输出的绝路路径,硬盘上不存在这个路径,会报错吗?

- 1️⃣ 注意下面文件
userFilesTest.txt文件的路径:D:\CANoe-Demo\TestModule\FilePathFunc

- 2️⃣ 我们执行下面的脚本,注意脚本文件名是:
filePathFuncTest.can和userFilesTest.txt同级目录
on key 'e'
{
char absPath[256];
long retVal;
retVal = getAbsFilePath("userFilesTest.txt", absPath, 256);
write ("absPath: %s ", absPath);
write ("retVal: %d ", retVal);
}
- 3️⃣ 输出结果显示函数返回值是
31,这表明返回值绝对路径的字符串长度;另外范围值是D:\CANoe-Demo\userFilesTest.txt,不是预期的D:\CANoe-Demo\TestModule\FilePathFunc\userFilesTest.txt - 重点来了:
1:返回值的路径在本机上不存在,不会报错;
2:组成绝对的根目录不是以这个脚本为参考根目录,而是以这个cfg文件所在文件夹为根目录

- 4️⃣ 所以我们想要获得准确的绝对路径名,就要以cfg所在文件为根目录,然后写对相对路径,如下图代码:
on key 'f'
{
char absPath[256];
long retVal;
retVal = getAbsFilePath("TestModule\\FilePathFunc\\userFilesTest.txt", absPath, 256);
write ("absPath: %s ", absPath);
write ("retVal: %d ", retVal);
}

setFilePath
- .
void setFilePath (char Path[],dword mode); - 根据下面
help文档的解释,就是在有对文件操作的时候要先设置要操作的文件路径 - 那么问题来了,我读写文件的时候,这个函数一定需要吗?不写会怎么样?

- 1️⃣ 看下下面文件的路径位置

- 2️⃣ 我们就不加
setFilePath函数,直接操作这个test.ini文件会怎样呢?
on key 'g'
{
long retVal;
//写入INI文件 函数
retVal = writeProfileInt ("setting", "parameter_1", 8, "test.ini");
write ("retVal: %d ", retVal);
}
- 3️⃣ 输出结果显示函数返回值是
1,这表明函数执行成功了,但是test.ini文件中什么信息都没有呢 - 重点来了:
1:返回值的路径在本机上不存在,不会报错;
2:组成绝对路径的根目录不是以这个脚本为参考根目录,而是以这个cfg文件所在文件夹为根目录

- 4️⃣ 在 打开的cfg的根目录下发现了test.ini文件,并且写入了值,这就说明,如果直接输入文件名,如果cfg所在文件下没有这个这个文件,那么会直接创建写入。

- 5️⃣ OK,直接写文件名,找不到路径,那我填入绝对路径总可以了吧
on key 'h'
{
long retVal;
//写入INI文件 函数
// 直接写入绝对路径?
retVal = writeProfileInt ("setting", "parameter_1", 8, "D:\\CANoe-Demo\\TestModule\\FilePathFunc\\test.ini");
write ("retVal: %d ", retVal);
}
- 6️⃣ 纳尼?返回值为0,这也不行;原来如果没有提前设置文件要操作的路径,必须是相对路径,绝对路径都不行。


- 7️⃣ 这回我们设置成相对路径
on key 'j'
{
long retVal;
//写入INI文件 函数
// 如果没有提前设置文件操作路径,必须用相对路径
retVal = writeProfileInt ("setting", "parameter_1", 8, "TestModule/FilePathFunc/test.ini");
write ("retVal: %d ", retVal);
}
- 8️⃣ 果然成功了,但是如果我要操作的文件不在D盘,也和cfg文件没有相对关系,那怎么弄?,好像还必须要要用
setFilePath或者setWritePath.才行
- 9️⃣ 真不服气,拿到真的没办法了吗?必须要妥协先设置
setFilePath?看下图,还有一种方法,
Configuration|Options|Extensions|User Files 把要操作的文件加进去,
这里注意,不可以添加文件名相同的问价,即使路径不同
- 再直接操作文件名
on key 'l'
{
long retVal;
retVal = writeProfileInt ("setting", "parameter_1", 8, "test.ini");
write ("retVal: %d ", retVal);
}
- 测试结果如下图,也是能够成功的;

- 1️⃣1️⃣ 经过了前面步骤的试错,为了代码的适用性,我们还是不要挣扎了 ,还是最好加上
setFilePath函数
on key 'k'
{
long retVal;
setFilePath("E:\\FilePathFunc", 1);
retVal = writeProfileInt ("setting", "parameter_1", 8, "test.ini");
write ("retVal: %d ", retVal);
}
- 1️⃣2️⃣ 测试结果如下图;

setWritePath
setWritePath 是 setFilePath 的功能子集
getUserFilePath
- .看下图help文档

1️⃣ 这个函数和
getAbsFilePath功能很像,但是又不同,比如比如下面的代码getUserFilePath首先在 user files 里面(上面截图中,添加进去的)寻找,找到了,就返回这个文件的绝对路径
找不到就去打开的cfg文件所在的问价夹目录下去找getAbsFilePath直接去打开的cfg文件所在的问价夹目录下去找,找不到那就报错了。
on key 'z'
{
char absPath[256];
long retVal;
retVal = getUserFilePath("test.ini", absPath, 256);
write ("getUserFilePath absPath: %s ", absPath);
write ("getUserFilePath retVal: %d ", retVal);
retVal = getAbsFilePath("test.ini", absPath, 256);
write ("getAbsFilePath absPath: %s ", absPath);
write ("getAbsFilePath retVal: %d ", retVal);
}
- 2️⃣ 看下输出结果,有空的可以把user file在 option中去掉再试试看看什么结果

RegisterUserFile
- .看下图help文档,就是以代码的方式在 CANoe运行期间也可以在 user Files中添加文件

- 1️⃣ 直接看代码,执行看下结果
on key 'x'
{
char absPath[256];
long retVal;
retVal = RegisterUserFile("E:\\FilePathFunc\\test2.ini",0);
write ("getUserFilePath retVal: %d ", retVal);
retVal = getUserFilePath("test2.ini", absPath, 256);
write ("getUserFilePath absPath: %s ", absPath);
write ("getUserFilePath retVal: %d ", retVal);
}
- 2️⃣ 停止
CANoe的运行,看下输出结果和User Files配置里已经自动把test2.ini文件添加进来了




总结

- 要有最朴素的生活,最遥远的梦想,即使明天天寒地冻,路遥马亡!
- 有微信的小伙伴可以关注下浪哥车载诊断,一个行业内小小圈子,群里有
网盘资料,源码,还有各路大神闲时交流交流技术,聊聊工作机会啥的。- 如果这篇博客对你有帮助,请 “点赞” “评论”“收藏”一键三连 哦!码字不易,大家的支持就是我坚持下去的动力。
边栏推荐
- Publish and subscribe to redis
- How does the single chip microcomputer execute the main function from power on reset?
- What are the models of data modeling
- 美团二面:为什么 Redis 会有哨兵?
- There are software load balancing and hardware load balancing. Which one to choose?
- 51单片机进修的一些感悟
- [Yu Yue education] reference materials of power electronics technology of Jiangxi University of science and technology
- [deep learning] semantic segmentation: thesis reading (neurips 2021) maskformer: per pixel classification is not all you need
- Withdrawal of wechat applet (enterprise payment to change)
- May brush question 27 - figure
猜你喜欢

MapReduce instance (x): chainmapreduce

【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need

小白带你重游Spark生态圈!

Full stack development of quartz distributed timed task scheduling cluster

嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历
![[deep learning] semantic segmentation: paper reading: (2021-12) mask2former](/img/dd/fe2bfa3563cf478afe431ac87a8cb7.png)
[deep learning] semantic segmentation: paper reading: (2021-12) mask2former

Take you back to spark ecosystem!

Servlet learning diary 8 - servlet life cycle and thread safety

Hero League rotation chart manual rotation

Nc17 longest palindrome substring
随机推荐
为拿 Offer,“闭关修炼,相信努力必成大器
[deep learning] semantic segmentation: paper reading: (2021-12) mask2former
C#/. Net phase VI 01C Foundation_ 01: running environment, process of creating new C program, strict case sensitivity, meaning of class library
单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
How does the single chip microcomputer execute the main function from power on reset?
【深度学习】语义分割-源代码汇总
[deep learning] semantic segmentation - source code summary
Counter attack of noodles: redis asked 52 questions in a series, with detailed pictures and pictures. Now the interview is stable
Lua script of redis
手把手教您怎么编写第一个单片机程序
51单片机进修的一些感悟
美团二面:为什么 Redis 会有哨兵?
One article read, DDD landing database design practice
Mapreduce实例(七):单表join
Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
小白带你重游Spark生态圈!
068.查找插入位置--二分查找
Redis distributed lock implementation redison 15 questions
一文读懂,DDD落地数据库设计实战
