stm32 Fats 文件系统移植、应用

这里写图片描述
移植红牛开发板的 文件系统 到rtos 工程中。
F_open 形参:
Flags Meaning
FA_READ Specifies read access to the object. Data can be read from the file.
FA_WRITE Specifies write access to the object. Data can be written to the file. Combine with FA_READ for read-write access.
FA_OPEN_EXISTING Opens the file. The function fails if the file is not existing. (Default)
FA_CREATE_NEW Creates a new file. The function fails with FR_EXIST if the file is existing.
FA_CREATE_ALWAYS Creates a new file. If the file is existing, it will be truncated and overwritten.
FA_OPEN_ALWAYS Opens the file if it is existing. If not, a new file will be created.
FA_OPEN_APPEND Same as FA_OPEN_ALWAYS except the read/write pointer is set end of the file.

POSIX FatFs
“r” FA_READ
“r+” FA_READ | FA_WRITE
“w” FA_CREATE_ALWAYS | FA_WRITE
“w+” FA_CREATE_ALWAYS | FA_WRITE | FA_READ
“a” FA_OPEN_APPEND | FA_WRITE
“a+” FA_OPEN_APPEND | FA_WRITE | FA_READ
“x”*1 FA_CREATE_NEW | FA_WRITE
“x+”*1 FA_CREATE_NEW | FA_WRITE | FA_READ

fopen相关参数:
“r” 以只读方式打开文件,该文件必须存在。
“r+” 以可读写方式打开文件,该文件必须存在。
“w” 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
“w+” 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
“a” 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
”a+“ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留)
SEEK_SET 从距文件开头offset 位移量为新的读写位置.
SEEK_CUR 以目前的读写位置往后增加offset个位移量.
2)SEEK_END 将读写位置指向文件尾后再增加offset 个位移量. 当whence 值为SEEK_CUR 或SEEK_END 时, 参数offset 允许负值的出现.

附加说明:fseek()不像lseek()会返回读写位置, 因此必须使用ftell()来取得目前读写的位置.
Ftell() 用于得到文件位置指针当前位置相对于文件首的偏移字节数。
fseek(fp, 0L,SEEK_END);
len =ftell(fp); 首先将文件的当前位置移到文件的末尾,然后调用函数ftell()获得当前位置相

例子:创建1个新文件,往文件中写人数据, 再读取数据
注意读取数据时,f_open(&fd, “file.bin”, FA_READ ); 从头开始的

FATFS    fatfs;
FIL      fd;
FRESULT  result;
unsigned int      len ;
unsigned char  readbuffer[50];

 void  lxl_prac_fileoperation()
{
   result = f_mount(&fatfs, "", 0);        //加载文件系统
    if (result != FR_OK) 
    {
        printf(" fmount file err \r\n");
    }
    else if(result == FR_OK)
    {
        printf("fmount suncessful \r\n");
    }


   result = f_open(&fd, "file.bin", FA_CREATE_NEW);

   result = f_write(&fd,"hello fatfs", sizeof("hello fatfs"), &len);
     f_close(&fd); 

      result = f_open(&fd, "file.bin", FA_READ );//注 FA_READ 指向文件的 0位置
     if(result!=FR_OK)
     {
        printf("f_open file.bin err \r\n");

     } 
     result = f_read(&fd,readbuffer,sizeof("hello fatfs"),&len);   
     readbuffer[len] ='\0';      

     printf("%s \r\n",readbuffer);

     f_close(&fd);

     f_mount(NULL, "", 0);        //加载文件系统

}

f_lseek 应用:
 res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
    if (res) ...

    /* Move to offset of 5000 from top of the file */
    res = f_lseek(fp, 5000);

    /* Move to end of the file to append data */
    res = f_lseek(fp, f_size(fp));

    /* Forward 3000 bytes */
    res = f_lseek(fp, f_tell(fp) + 3000);

    /* Rewind 2000 bytes (take care on wraparound) */
res = f_lseek(fp, f_tell(fp) - 2000);

// 在原来文件后,填件内容

void  lxl_prac_fileoperation2()
{
   result = f_mount(&fatfs, "", 0);        //加载文件系统
    if (result != FR_OK) 
    {
        printf(" fmount file err \r\n");
    }
    else if(result == FR_OK)
    {
        printf("fmount suncessful \r\n");
    }

  result = f_open(&fd, "file.bin",   FA_CREATE_ALWAYS | FA_WRITE | FA_READ) ; // 根据前面的表,知道相当于 w+ (w 破坏原文件,重新写,w+ 不破坏原文件,但文件指针在头部,借用 f_lseek(fp, f_size(fp)) 移到函数末尾)
    if(result != FR_OK)
  {
         printf("failed to open file mode is FA_READ|FA_WRITE| FA_OPEN_ALWAYS!\r\n");
  }

      len= f_tell(&fd);
      printf("size is %d \r\n",len);

       f_lseek(&fd,20); // 移到文件第20个字节处

      f_write(&fd,"i will add data in fatfs", sizeof("i will add data in fatfs"), &len);//在文件末尾+20 字节处 添加内容

      f_close(&fd);

       result = f_open(&fd, "file.bin", FA_READ);  // 移到文件的首位置
        if(result != FR_OK)
     {
             printf("failed to open file!\r\n");
     }


         result = f_read(&fd,readbuffer,50,&len);   
     readbuffer[49] ='\0';      

      printf("%s \r\n",readbuffer);

     f_close(&fd);

        f_mount(NULL, "", 0);        // 卸载文件系统
} 

文件系统练习:

这个函数 是输入posix 接口值,获得 fatfs 的open 类型:
int  fopenString(  char * temp)
{
     int  val =0;

       if(strncmp((char *)temp,"r+",2) ==0)
     {
            val =FA_READ | FA_WRITE ;
            return val ;
     }
     else  if(strncmp((char *)temp,"r",1) ==0)
     {
          val =FA_READ ;
            return val ;         
     }


      if (strncmp((char *)temp,"w+",2) ==0)
     {
          val =FA_CREATE_ALWAYS | FA_WRITE | FA_READ ;
            return val ;
     }
     else  if(strncmp((char *)temp,"w",1) ==0)
     {
         val =FA_CREATE_ALWAYS | FA_WRITE;
            return val ;
     }

     else 
     {

        return -1;
     }

}


#define SD_SECTOR_SIZE    (512)
#define BUFF_SIZE         (4096)

FATFS   fs0;
FIL     f0;
FRESULT fr;
DIR     dir0;
FILINFO f_info;


void  fileoperation()
{
   result = f_mount(&fatfs, "", 0);        //加载文件系统
    if (result != FR_OK) 
    {
        printf(" fmount file err \r\n");
    }
    else if(result == FR_OK)
    {
        printf("fmount suncessful \r\n");
    }

     result = f_open(&fd,"file.bin",fopenString("w"));// 将文件内容清零

     f_puts("fopen(w)",&fd);
     f_close(&fd);


    result = f_open(&fd, "file.bin", fopenString("r+") ); 打开文件,指针在文件头,准备写(文件必须存在)


       unsigned char tempstring[20]="file operation\r\n";
       f_puts((const TCHAR*)tempstring,&fd); // 前面的内容将被覆盖

 // /f_lseek(&fd,f_size(&fd)); // 移到文件末尾  ,如果文件不想被覆盖,加上这句话

    //len =f_tell(&fd); // 获取当前指针位置
        f_puts((const TCHAR*)tempstring,&fd);

        f_close(&fd);


      result = f_open(&fd, "file.bin",   fopenString("r"));  // 移到文件的首位置,读文件
        if(result != FR_OK)
     {
             printf("failed to open file!\r\n");
     }

         result = f_read(&fd,readbuffer,100,&len);   
     readbuffer[99] ='\0';      

      printf("%s \r\n",readbuffer);  
        f_close(&fd); 
        f_mount(NULL, "", 0);        // 卸载文件系统
}

运行结果:
fmount suncessful
file operation
file operation

如果去掉 f_lseek(&fd,f_size(&fd)); 注释,运行结果:
fmount suncessful
fopen(w)file operation
file operation

猜你喜欢

转载自blog.csdn.net/tiger15605353603/article/details/81380291