[mw_shl_code=c,true] u32 len,len_rw;
char buff[]={"test_file write"};
FRESULT result_Udisk;
len_rw = sizeof("test_file write");
result_Udisk = f_mount(fs,(const TCHAR*)"0:",1); //Mount a logical drive
if(FR_OK ==result_Udisk){
result_Udisk = f_open(file,"0:/test_file.txt",FA_WRITE|FA_READ);
if(FR_OK==result_Udisk){
result_Udisk = f_write(file,buff,len_rw,&len);
if(FR_OK==result_Udisk){
memset((u8*)buff,0,sizeof(buff));
result_Udisk = f_read(file,buff,16,&len);
if(FR_OK==result_Udisk){
if(memcmp(buff,"test_file write",sizeof("test_file write"))==0){
printf("test_file OK
");
}
}
}
}
}
f_close(file);[/mw_shl_code]
以上是写的测试代码,用的是nandfalsh作为磁盘,先执行写文件,然后再读取出来,
读的数据长度返回是0,单步仿真发现在读的函数里面出现问题。下面贴部分代码。
如果只执行读,去掉写那句,则读取正常。文件是存在的
[mw_shl_code=c,true]FRESULT f_read (
FIL* fp, /* Pointer to the file object */
void* buff, /* Pointer to data buffer */
UINT btr, /* Number of bytes to read */
UINT* br /* Pointer to number of bytes read */
)
{
FRESULT res;
FATFS *fs;
DWORD clst, sect;
FSIZE_t remain;
UINT rcnt, cc, csect;
BYTE *rbuff = (BYTE*)buff;
*br = 0; /* Clear read byte counter */
res = validate(fp, &fs);
if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res); /* Check validity */
if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */
remain = fp->obj.objsize - fp->fptr;//这句remain =0 了 ,因为fp->obj.objsize 和fp->fptr 相等
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ //执行到这里。 这 btr 就已经是0了 所以下面的for,就直接退出了
for ( ; btr; /* Repeat until all data read */
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) [/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>