FATFS 的几个函数用法

2318阅读 0评论2011-07-04 yuweixian4230
分类:嵌入式

FATFS 的几个函数用法:

 

 FATFS *fs, fatfs;

 fs = &fatfs;
 f_mount(0, fs);

 b = f_open(&infile,"SD.txt",FA_CREATE_NEW);    //创建新文件
 f_close(&infile);    //关闭文件

 b = f_open(&infile,"SD.txt", FA_WRITE);   //以写方式打开文件
 f_puts((char *)buff2,&infile);  //文件内写入字符串
 f_puts((char *)buff2,&infile);  //文件内写入字符串
 f_puts((char *)buff2,&infile); //文件内写入字符串
 f_close(&infile);  //关闭文件

 b = f_open(&infile,"SD.txt",FA_WRITE);   //以写方式打开文件
 b = infile.fsize;       //获得文件大小
 f_lseek(&infile,b);  //移动文件指针
 f_puts(buff3,&infile); //从文件内数据的最后写入字符串
 f_close(&infile);    //关闭文件

 b = f_open(&infile,"SD.txt",FA_READ);  //以读方式打开文件
 f_read(&infile,buff1,50,&rc);  //从文件内读50字节赋给 buff1数组
 f_close(&infile);  //关闭文件

// f_unlink("SD.txt");  //删除文件

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 FATFS fs;            // Work area (file system object) for logical drive
 FIL fileuse;      // file objects
 unsigned char buffer[512]; // file copy buffer
 unsigned int nfr, nfw;         // File R/W count
void TestFATFS_Read(void)
{
  //FILINFO finfo;
  //DIR dirs;
  //char name[]={"TEST.TXT"};
  uint8_t result;
  char path[50]={"0:/SD/BIN.TXT"};  

  printf("********* Test FATFS Read begin*************\n"); 
  f_mount(0, &fs); 
  result = result;

  if( (f_open(&fileuse,path, FA_OPEN_EXISTING |FA_READ))!=FR_OK) {printf("f_open() Failed !\n");return;}//以读方式打开文件       
  if( (f_read(&fileuse, buffer, sizeof(buffer), &nfr))!=FR_OK)   {printf("f_read() Failed !\n");return;}
  printf("%s\n",buffer);
  f_close(&fileuse);

  printf("********* Test FATFS Read over*************\n");
}

void TestFATFS_Write(void)
{
  char path[50]={"0:/SD/BIN.TXT"};  
  unsigned char cont[]={"read write OK !"};
  printf("********* Test FATFS Write begin*************\n"); 
  f_mount(0, &fs); 
 
  if( (f_open(&fileuse,path, FA_OPEN_ALWAYS |FA_WRITE))!=FR_OK) {printf("f_open() Failed !\n");return;}//以读方式打开文件       
  if( (f_write(&fileuse, cont, sizeof(cont), &nfr))!=FR_OK)   {printf("f_read() Failed !\n");return;}
  f_close(&fileuse);

  printf("********* Test FATFS Write over*************\n");
}


http://hi.baidu.com/mem2005/blog/item/e4343ba2480fc7b5caefd0fc.html

上一篇:移植fatfs文件系统成功 3
下一篇:fatfs 从SD卡中读数据 (4)