eric2013 发表于 2021-1-20 08:17:52

ThreadX FileX打开一个老文件,调用写数据函数fx_file_write,默认是从文件末尾开始调用,需调用函数fx_file_seek设置


测试代码:
/*
*********************************************************************************************************
*        函 数 名: CreateNewFile
*        功能说明: 在SD卡创建一个新文件,文件内容填写“www.armfly.com”
*        形    参:无
*        返 回 值: 无
*********************************************************************************************************
*/
static void CreateNewFile(void)
{
    UINT status;

   
    /* 挂载SD卡 */
    status =fx_media_open(&sdio_disk, "STM32_SDIO_DISK", fx_stm32_sd_driver, 0, media_memory, sizeof(media_memory));

    if (status != FX_SUCCESS)
    {
      printf("挂载文件系统失败 -- %d\r\n", status);
      return;
    }
   
    /* 创建文件 */
    status =fx_file_create(&sdio_disk, "armfly.txt");

    /* 检测状态 */
    if (status != FX_SUCCESS)
    {
      /* 失败的话,可以考虑二次创建 */
      if (status != FX_ALREADY_CREATED)
      {
            printf("创建文件失败\r\n");
      }
    }
   
    /* 打开文件*/
    status =fx_file_open(&sdio_disk, &fx_file, "armfly.txt", FX_OPEN_FOR_WRITE);

    if (status != FX_SUCCESS)
    {
      printf("打开文件失败\r\n");
    }
   
    /* 设置到起始位置读取*/
    status =fx_file_seek(&fx_file, 0);

    if (status != FX_SUCCESS)
    {
      printf("设置读取位置失败\r\n");      
    }


    /* 写入字符串到文件*/
    status =fx_file_write(&fx_file, FsWriteBuf, strlen(FsWriteBuf));

    if (status == FX_SUCCESS)
    {
      printf("armfly.txt 文件写入成功\r\n");      
    }
    else
    {
      printf("armfly.txt 文件写入失败 %d\r\n", status);
    }

    /* 关闭文件*/
    status =fx_file_close(&fx_file);

    /* Check the file close status.*/
    if (status != FX_SUCCESS)
    {
      printf("关闭文件失败\r\n");   
    }

    /* 保证文件写入全部生效 */
    status = fx_media_flush(&sdio_disk);

    if (status != FX_SUCCESS)
    {
      printf("flush失败\r\n");   
    }
   
   /* 卸载SD卡 */
    status =fx_media_close(&sdio_disk);

    if (status != FX_SUCCESS)
    {
      printf("卸载文件系统卸载失败 -- %d\r\n", status);
    }   
}


页: [1]
查看完整版本: ThreadX FileX打开一个老文件,调用写数据函数fx_file_write,默认是从文件末尾开始调用,需调用函数fx_file_seek设置