硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2735|回复: 0
收起左侧

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

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106749
QQ
发表于 2021-1-20 08:17:52 | 显示全部楼层 |阅读模式

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

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

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

  23.     /* 检测状态 */
  24.     if (status != FX_SUCCESS)
  25.     {
  26.         /* 失败的话,可以考虑二次创建 */
  27.         if (status != FX_ALREADY_CREATED)
  28.         {
  29.             printf("创建文件失败\r\n");
  30.         }
  31.     }
  32.    
  33.     /* 打开文件  */
  34.     status =  fx_file_open(&sdio_disk, &fx_file, "armfly.txt", FX_OPEN_FOR_WRITE);

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

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


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

  48.     if (status == FX_SUCCESS)
  49.     {
  50.         printf("armfly.txt 文件写入成功\r\n");        
  51.     }
  52.     else
  53.     {
  54.         printf("armfly.txt 文件写入失败 %d\r\n", status);
  55.     }

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

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

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

  65.     if (status != FX_SUCCESS)
  66.     {
  67.         printf("flush失败\r\n");   
  68.     }
  69.    
  70.      /* 卸载SD卡 */
  71.     status =  fx_media_close(&sdio_disk);

  72.     if (status != FX_SUCCESS)
  73.     {
  74.         printf("卸载文件系统卸载失败 -- %d\r\n", status);
  75.     }   
  76. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-4 03:03 , Processed in 0.180190 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表