| 007 | #define F_READ 1 //测试从文件中读出数据 |
| 008 | #define F_UNLINK 0 //测试删除文件 |
| 009 | #define SCAN_FILES 1 //测试目录扫描 |
| 015 | BYTEbuffer[4096];//以上变量作为全局变量 可以避免一些Bug |
| 025 | USART_Configuration(); |
| 034 | //如果data.txt存在,则打开;否则,创建一个新文件 |
| 035 | res = f_open(&file,"0:/data.txt",FA_OPEN_ALWAYS|FA_READ|FA_WRITE ); |
| 039 | printf("f_open() fail .. "); |
| 041 | printf("f_open() success .. "); |
| 046 | while(1){//使用f_read读文件 |
| 047 | res = f_read(&file, buffer, 1, &br);//一次读一个字节知道读完全部文件信息 |
| 053 | printf("f_read() fail .. "); |
| 056 | if(f_eof(&file)) {break;} |
| 059 | /*if( f_gets(buffer,sizeof(buffer),&file) != NULL) //使用f_gets读文件 ,存在 Bugs 待调试 |
| 063 | printf("f_gets() fail .. "); |
| 071 | //res = f_lseek(&file,(&file)->fsize); |
| 072 | res = f_lseek(&file,file.fsize); |
| 074 | n = f_puts("hello dog ..", &file) ;//向文件末写入字符串 |
| 078 | printf("f_puts() fail .. "); |
| 080 | printf("f_puts() success .. "); |
| 087 | res = f_unlink("test.jpg");//前提SD下存在一个test.jpg |
| 091 | printf("f_unlink() fail .. "); |
| 093 | printf("f_unlink() success .. "); |
| 100 | printf("the directory files : "); |
| 101 | scan_files("/");//扫描根目录 |
| 113 | char* path/* Start node to be scanned (also used as work area) */ |
| 120 | char*fn;/* This function is assuming non-Unicode cfg. */ |
| 122 | staticcharlfn[_MAX_LFN + 1]; |
| 124 | fno.lfsize =sizeoflfn; |
| 128 | res = f_opendir(&dir, path);/* Open the directory */ |
| 132 | res = f_readdir(&dir, &fno);/* Read a directory item */ |
| 133 | if(res != FR_OK || fno.fname[0] == 0)break;/* Break on error or end of dir */ |
| 134 | if(fno.fname[0] ==.)continue;/* Ignore dot entry */ |
| 136 | fn = *fno.lfname ? fno.lfname : fno.fname; |
| 140 | if(fno.fattrib & AM_DIR) {/* It is a directory */ |
| 141 | sprintf(&path[i],"/%s", fn); |
| 142 | res = scan_files(path); |
| 143 | if(res != FR_OK)break; |
| 145 | }else{/* It is a file. */ |
| 146 | printf("%s/%s ", path, fn); |
其中 目录扫描函数 scan_files( char * path) 参数格式如下:

这里使用到了f_puts()函数,所以必须在ffconf.h 中修改 #define _USE_STRFUNC 1