Re: 【求助】【tuyaos】uf_db方式存储数据失败
wxq1123 2024年 Jan 16日 09:47yangjie 2024年 Jan 15日 17:33你看下这个内容:https://www.cnblogs.com/kangjianwei101/p/5220021.html,覆盖二进制写用 wb+,追加二进制写用 ab+
示例代码:
Code: Select all
*********************************************************** ***********************function define********************** ***********************************************************/ /** * @brief base db task * * @param[in] param:Task parameters * @return none */ VOID example_uf_file(INT_T argc, CHAR_T *argv[]) { OPERATE_RET op_ret = OPRT_OK; UCHAR_T * fd = NULL; UCHAR_T write_buff[5] = {0x00, 0x01, 0x02, 0x03, 0x04}; UCHAR_T *read_buff = NULL; INT_T write_num = 0; INT_T read_num = 0; BOOL_T temp = TRUE; TAL_PR_NOTICE("------ uf file example start ------"); /*open file, read and write mode*/ fd = ufopen("test.txt", "wb+"); // 覆盖写,追加写用 "ab+" if(NULL == fd) { TAL_PR_ERR("file open fail"); } else { TAL_PR_DEBUG("fd : %d", *fd); } /*change offset*/ ufseek(fd, 0, UF_SEEK_END); /*write*/ write_num += ufwrite(fd, write_buff, 5); TAL_PR_DEBUG("write number : %d", write_num); /*check if the current offset reach the end of the file*/ temp = ufeof(fd); if(temp) { TAL_PR_DEBUG("offset is at the end"); } else { TAL_PR_DEBUG("offset is not at the end"); } /*change offset*/ ufseek(fd, 0, UF_SEEK_SET); /*get current offset of the unique file*/ op_ret = uftell(fd); TAL_PR_DEBUG("current offset : %d", op_ret); /*get the size of the unique file*/ op_ret = ufgetsize("test.txt"); TAL_PR_DEBUG("file size: %d", op_ret); if (op_ret > 0) { write_num = op_ret; } /*read*/ read_buff = (UCHAR_T *)tal_malloc(sizeof(CHAR_T)*write_num + 1); if(NULL == read_buff) { TAL_PR_ERR("read buff malloc fail!"); } read_num = ufread(fd, read_buff, write_num); for (int i =0; i<read_num; i++) { TAL_PR_DEBUG("read_buff[%d} : %x", i, read_buff[i]); } /*close*/ op_ret = ufclose(fd); if(OPRT_OK != op_ret) { TAL_PR_ERR("err<%d>, close fail!", op_ret); } tal_free(read_buff); return; }
wb+ ab+ 模式打开均失败。微信截图_20240116094635.jpg
不好意思,涂鸦的 文件系统仅支持 "r","r+","a","a+","w","w+" 模式。把 "b" 删掉就可以了。
下面的示例是跑过的,更简单的,你可以看下:
Code: Select all
VOID example_uf_file(INT_T argc, CHAR_T *argv[])
{
OPERATE_RET op_ret = OPRT_OK;
UCHAR_T * fd = NULL;
UCHAR_T write_buff[] = {0x00, 0x01, 0x02, 0x03, 0x04};
UCHAR_T *read_buff = NULL;
INT_T write_num = 0;
INT_T read_num = 0;
TAL_PR_NOTICE("------ uf file example start ------");
/* open file, read and write mode */
fd = ufopen("test.txt", "w+");
if(NULL == fd) {
TAL_PR_ERR("file open fail");
return;
} else {
TAL_PR_DEBUG("fd : %d", *fd);
}
/* write */
write_num = ufwrite(fd, write_buff, 5);
if (write_num != 5) {
TAL_PR_ERR("write error, write number : %d", write_num);
} else {
TAL_PR_DEBUG("write number : %d", write_num);
}
/* close */
op_ret = ufclose(fd);
if(OPRT_OK != op_ret) {
TAL_PR_ERR("err<%d>, close fail!", op_ret);
}
// 至此文件写内容已经结束了
// 下面是关于文件读的示例
fd = ufopen("test.txt", "r+");
if(NULL == fd) {
TAL_PR_ERR("file open fail");
return;
} else {
TAL_PR_DEBUG("fd : %d", *fd);
}
INT_T fileSize = ufgetsize("test.txt");
TAL_PR_DEBUG("file size: %d", fileSize);
read_buff = (UCHAR_T *)tal_malloc(fileSize);
if(NULL == read_buff) {
ufclose(fd);
TAL_PR_ERR("read buff malloc fail!");
return;
}
read_num = ufread(fd, read_buff, fileSize);
for (int i = 0; i < read_num; i++) {
TAL_PR_DEBUG("read_buff[%d] = 0x%02x", i, read_buff[i]);
}
if (NULL != read_buff) {
tal_free(read_buff);
read_buff = NULL;
}
/* close */
op_ret = ufclose(fd);
if(OPRT_OK != op_ret) {
TAL_PR_ERR("err<%d>, close fail!", op_ret);
}
return;
}
示例执行后日志如下:
Code: Select all
[01-01 00:00:12 ty N][example_os_uf_file.c:120] ------ uf file example start ------
[01-01 00:00:12 ty D][example_os_uf_file.c:128] fd : 1
[01-01 00:00:12 ty D][example_os_uf_file.c:136] write number : 5
[01-01 00:00:12 ty D][example_os_uf_file.c:153] fd : 3
[01-01 00:00:12 ty D][example_os_uf_file.c:157] file size: 5
[01-01 00:00:12 ty D][example_os_uf_file.c:168] read_buff[0] = 0x00
[01-01 00:00:12 ty D][example_os_uf_file.c:168] read_buff[1] = 0x01
[01-01 00:00:12 ty D][example_os_uf_file.c:168] read_buff[2] = 0x02
[01-01 00:00:12 ty D][example_os_uf_file.c:168] read_buff[3] = 0x03
[01-01 00:00:12 ty D][example_os_uf_file.c:168] read_buff[4] = 0x04
关于 tuyaos 中文件系统的接口和c语言中的使用方式基本相同,C语言的文件操作你可以看下这个https://www.cnblogs.com/likebeta/archiv ... 51780.html,但是 tuyaos 中的文件系统部分接口可能没有,但是使用方法、函数功能和上面链接中基本上是一样的。