加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

FATFS文件系统中f_lseek函数的使用

(2017-12-11 21:52:19)
标签:

fatfs

sd

stm32

分类: 技术类
FATFS文件系统中f_lseek函数的使用
BY:T7
Date:20171212
At:YSU_B307

开发环境:
uVision : V5.12.0.0 STM32F103V8T6
库版本  : STM32F10x_StdPeriph_Lib_V3.5.0
FATSF    :  ff13a  下载地址:http://elm-chan.org/fsw/ff/00index_e.html

工程功能:分两次在同一个文件中写入两句话
第一次写入“This is the FATFS System!\r\n”
第二次在同一个文件中,第一句的后面写入:“************************\r\n"”

一、官方给出的解释
f_lseek
The f_lseek function moves the file read/write pointer of an open file object. It can also be used to expand the file size (cluster pre-allocation).
FRESULT f_lseek (
  FIL*    fp,  
  FSIZE_t ofs  
);
Parameters
fp
Pointer to the open file object.
ofs
Byte offset from top of the file to set read/write pointer. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.

二、主函数源码:后面附上整个工程的百度云链接
#include "main.h"

#define ONE_BLOCK       512
#define TWO_BLOCK      1024

FIL *FilPtr,FileSystemDst; //文件系统结构体,包含文件指针等成员

UINT br,bw; //Fil R/W count
BYTE FileRxBuffer[4096]; //FILE COPY BUFFER
BYTE TxFileBuffer[] = "This is the FATFS System!\r\n";
BYTE TxFileBuffer1[] = "************************\r\n";

int main(void)
{
FATFS fs; //记录文件系统盘符信息的结构体
LED_Init();
USARTx_Init();
FilPtr = &FileSystemDst;

f_mount(&fs, "0:", 1 );

f_open( FilPtr , "0:/Demo0.TXT", FA_OPEN_ALWAYS | FA_WRITE);  
f_lseek(FilPtr,f_size(FilPtr)); //第一次该函数实际并没有发挥威力
f_write( FilPtr ,  TxFileBuffer  , sizeof(TxFileBuffer) , &bw); 
f_close(FilPtr); 

f_open( FilPtr , "0:/Demo0.TXT", FA_OPEN_ALWAYS | FA_WRITE);
f_lseek(FilPtr,f_size(FilPtr));
f_write( FilPtr ,  TxFileBuffer1  , sizeof(TxFileBuffer1) , &bw);
f_close(FilPtr);

while(1)
  { }
}



STM32上电四次,用读卡器打开SD卡的Demo.TXT文件,如下:

百度云链接:链接:https://pan.baidu.com/s/1bpxx4eF 密码:47vn


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有