fatfs 学习笔记--f_write用法(最新版本R0.13)

来源:互联网 发布:龙虎榜数据每天几点出 编辑:程序博客网 时间:2024/06/05 04:12

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

fp
Pointer to the open file object structure.
buff
Pointer to the data to be written.
btw
Specifies number of bytes to write in range of UINT type.
bw
Pointer 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 should be 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]);
    }
}




原创粉丝点击