This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3235S中如何正确使用Fs_Open()打开文件夹下的文件

Other Parts Discussed in Thread: UNIFLASH

您好,最近在使用OOB工程时,遇到一个棘手的问题,用UNiflash将文件添加到www文件夹目录下后,需要在代码中打开并读写www文件夹下面的那个文件,可是用Fs_Open()接口,却提示文件不存在。是代码有错吗?

char *DeviceFileName = "\www\test.txt";
long            DeviceFileHandle = -1;
_i32            fsRetVal;        //negative retval is an error
unsigned long   Offset = 0;
unsigned char   InputBuffer[100];
_u32            MasterToken = 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);

  • 参考下面的代码,创建文件并写入数据:

    char*           DeviceFileName = "MyFile.txt";
    unsigned long   MaxSize = 63 * 1024; //62.5K is max file size
    long            DeviceFileHandle = -1;
    _i32            RetVal;        //negative retval is an error
    unsigned long   Offset = 0;
    unsigned char   InputBuffer[100];
    _u32            MasterToken = 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);

  • 这个步骤操作过,可是这样新建的文件会存在根目录里面,没有在根目录的www文件夹下,请问如何将文件写到文件夹下呢?
  • 你是如何添加的,下图这样add file吗?

  • 我用的是UNiflash6.0版本的,但是已经找到解决方案了,是文件名那里的路径不对,应该是“/www/devname.txt”,谢谢

  • 解决就好,我猜也是添加文件方式不对
  • 和添加文件的方式没太大关系,主要是代码里面在加在文件名的地方应该是“/www/devname”而不是“\www\devname”,具体的可以查看SDK里面的Documentation_Overview.html文档
  • 收到,感谢提供反馈