fatfs 学习笔记--f_write用法(最新版本R0.13)
2017-07-15 10:44
483 查看
f_write
The f_write writes data to a file.FRESULT f_write ( FIL* fp, /* [IN] Pointer to the file object structure */ const void* buff, /* [IN] Pointer to the data to be written */ UINT btw, /* [IN] Number of bytes to write */ UINT* bw /* [OUT] Pointer to the variable to return number of bytes written */ );
Parameters
fpPointer to the open file object structure.buffPointer to the data to be written.
btwSpecifies number of bytes to write in range of UINT type.
bwPointer to the UINT variable to return the number of bytes written. This
value is always valid after the function call regardless of the return value.
Return Values
FR_OK, FR_DISK_ERR, FR_INT_ERR, FR_DENIED, FR_INVALID_OBJECT, FR_TIMEOUT
Description
The function starts to write data to the file at the position pointed by the read/write pointer. The read/write pointer advances as number of bytes written. After the function succeeded, *bw shouldbe checked to detect the disk full. In case of *bw < btw,
it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.
QuickInfo
Available when FF_FS_READONLY == 0.1. 在读操作过程中,一旦*br < btr 则读/写指针到达了文件结束位置.
2. 在写操作过程中,一旦*bw < btw,则意味着该卷已满.
函数实例:
/*
*********************************************************************************************************
* 函 数 名: Test_f_readwrite
* 功能说明: f_read和f_write函数测试
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
uint8_t Test_f_readwrite(void)
{
FRESULT fr, result;
FATFS fs;
FIL fsrc, fdst;
UINT br, bw;
BYTE buffer[4096]; /* File copy buffer */
float FinishPecent;
uint32_t Count = 0;
/* 第1步:先挂载文件系统*********************************************************************/
result = f_mount(&fs, "1:", 1); /* Mount a logical drive */
if (result != FR_OK)
{
printf("挂载文件系统失败 (%s)\r\n", FR_Table[result]);
}
else
{
printf("挂载文件系统成功 (%s)\r\n", FR_Table[result]);
}
/* 第2步:打开两个文件********************************************************************/
fr = f_open(&fsrc, "1:/txt/nel.txt", FA_OPEN_EXISTING | FA_READ);
if (fr != FR_OK)
{
printf("打开失败 (%s)\r\n", FR_Table[fr]);
}
else
{
printf("打开成功 (%s)\r\n", FR_Table[fr]);
}
fr = f_open(&fdst, "1:/txt/nel1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if (fr != FR_OK)
{
printf("打开失败 (%s)\r\n", FR_Table[fr]);
}
else
{
printf("打开成功 (%s)\r\n", FR_Table[fr]);
}
Count = 0;
FinishPecent = 0.0f;
/* 第3步:将文件nel.txt中的内容复制到nel1.txt里面****************************************/
for (;;)
{
fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* 从源文件中读4K数据 */
/* 串口打印复制率 */
Count = Count + 1;
FinishPecent = (float)(Count* 4096) / fsrc.fsize;
printf("错误类型 = %s 当前已经复制%.2f \r", FR_Table[fr], FinishPecent);
if (fr || br == 0) break; /* 出现错误或者已经读完 */
fr = f_write(&fdst, buffer, br, &bw); /* 从源文件读出的内容写入多目的文件中 */
if (fr || bw < br) break; /* 写入出错或者磁盘已满 */
}
printf("\r\n"); /* 换行 */
/* 关闭打开的两个文件 */
f_close(&fsrc);
f_close(&fdst);
/* 最后一步:卸载文件系统 */
result = f_mount(NULL, "1:", 1);
if (result != FR_OK)
{
printf("卸载文件系统失败 (%s)\r\n", FR_Table[result]);
}
else
{
printf("卸载文件系统成功 (%s)\r\n", FR_Table[result]);
}
}
相关文章推荐
- fatfs 学习笔记--f_open用法(最新版本)
- fatfs 学习笔记--f_mount用法
- 移植最新版本FATFS R0.13 到STM32F105RB的内部flash diskio.c源码
- 解决”正尝试安装的adobe flash player不是最新版本“的办法(学习笔记)
- Dynamic CRM 2013学习笔记(三十九)流程2 - 业务流程(Business Process Flows)用法详解
- STM32学习笔记3——怎样将整型变量转换为字符变量 然后串口 或者LCD发送出来(STM32) 以及sprintf的用法
- Java学习笔记(二)之―Java基础, Java平台版本, JDK环境搭建
- Python学习笔记2--Python版本选择
- 微软企业库5.0学习笔记(二)关于5.0版本
- Ext.Net学习笔记20:Ext.Net FormPanel 复杂用法
- [知了堂学习笔记]_ 正则表达式的组成及在Java中常见正则表达式的用法
- TensorFlow学习笔记--1.0 版本的一个小坑记录
- Java学习笔记-适配器用法
- [知了堂学习笔记]_break的用法
- static关键字用法学习笔记(C++)
- 【Java学习笔记之十二】Java8增强的工具类:Arrays的用法整理总结
- 【APUE学习笔记】dup和dup2用法
- Git学习笔记 chapter02--版本回退
- 学习笔记:Structs详细用法(二)------MLDN