您好,最近在使用OOB工程时,遇到一个棘手的问题,用UNiflash将文件添加到www文件夹目录下后,需要在代码中打开并读写www文件夹下面的那个文件,可是用Fs_Open()接口,却提示文件不存在。是代码有错吗?
char *DeviceFileName = "\www\test.txt"; longDeviceFileHandle = -1; _i32fsRetVal;//negative retval is an error unsigned longOffset = 0; unsigned charInputBuffer[100]; _u32MasterToken = 0; memset(InputBuffer,0,sizeof(InputBuffer)); DeviceFileHandle = sl_FsOpen((unsigned char *)DeviceFileName, SL_FS_READ, &MasterToken); fsRetVal = sl_FsRead( DeviceFileHandle, Offset, (unsigned char *)InputBuffer,50); fsRetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
Viki Shi:
参考下面的代码,创建文件并写入数据:
char*DeviceFileName = "MyFile.txt"; unsigned longMaxSize = 63 * 1024; //62.5K is max file size longDeviceFileHandle = -1; _i32RetVal;//negative retval is an error unsigned longOffset = 0; unsigned charInputBuffer[100]; _u32MasterToken = 0; // Create a file and write data. The file in this example is secured, without signature and with a fail safe commit //create a secure file if not exists and open it for write. DeviceFileHandle =sl_FsOpen(unsigned char *)DeviceFileName,SL_FS_CREATE|SL_FS_OVERWRITE | SL_FS_CREATE_SECURE | SL_FS_CREATE_NOSIGNATURE | SL_FS_CREATE_MAX_SIZE( MaxSize ),&MasterToken); Offset = 0; //Preferred in secure file that the Offset and the length will be aligned to 16 bytes. RetVal = sl_FsWrite( DeviceFileHandle, Offset, (unsigned char *)"HelloWorld", strlen("HelloWorld")); RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0); // open the same file for read, using the Token we got from the creation procedure above DeviceFileHandle =sl_FsOpen(unsigned char *)DeviceFileName,SL_FS_READ,&MasterToken); Offset = 0; RetVal = sl_FsRead( DeviceFileHandle, Offset, (unsigned char *)InputBuffer, strlen("HelloWorld")); RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
user4696099:
回复 Viki Shi:
这个步骤操作过,可是这样新建的文件会存在根目录里面,没有在根目录的www文件夹下,请问如何将文件写到文件夹下呢?
Viki Shi:
回复 user4696099:
你是如何添加的,下图这样add file吗?
Viki Shi:
回复 user4696099:
解决就好,我猜也是添加文件方式不对
user4696099:
回复 Viki Shi:
和添加文件的方式没太大关系,主要是代码里面在加在文件名的地方应该是“/www/devname”而不是“\www\devname”,具体的可以查看SDK里面的Documentation_Overview.html文档
Viki Shi:
回复 user4696099:
收到,感谢提供反馈