关于fatfs文件写操作,有个小问题困扰我很长时间了,请教一下 f_open(&MyFile, (char *)Node, FA_WRITE); f_write(&MyFile, UID, 5, (void *)&byteswritten); f_write(&MyFile, ID, 9, (void *)&byteswritten); f_write(&MyFile, Msg, 9, (void *)&byteswritten); f_write(&MyFile, &myGPS, 39, (void *)&byteswritten); f_write(&MyFile, "\r\n", 2, (void *)&byteswritten); f_close(&MyFile); f_open(&MyFile, (char *)Node, FA_WRITE); f_lseek(&MyFile,f_size(&MyFile)); f_write(&MyFile, UID, 5, (void *)&byteswritten); f_write(&MyFile, ID, 9, (void *)&byteswritten); f_write(&MyFile, Msg, 9, (void *)&byteswritten); f_write(&MyFile, &myGPS, 39, (void *)&byteswritten); f_write(&MyFile, "\r\n", 2, (void *)&byteswritten); f_close(&MyFile); 其实我主要是想用f_lseek函数,关闭文件之后下次再写时从结尾开始写,但是我才用上面的方法测试了好几遍,关闭文件前的内容能写进去,但是关闭之后再打开怎么也写不进去,调试时我在每行语句前加了res= ,来看res,都返回FR_OK,但是U盘里没有后面的内容,请问是什么情况? Node="0:/BJUT/TEST.TXT" |
查一下f_size(&MyFile)是多少?还有你写的东西宏定义还是啥,没看到。
先用f_open打开
再用f_lseek找到并定位到文件尾部,注意fsrc3.fsize这个输入形参
然后就可以f_write将数据写到文件尾部接着了
不是宏定义,那是字符数组的名字
我觉得关键在于:f_open(&fsrc3,"历史.csv",FA_OPEN_EXISTING | FA_WRITE)
这里形参要多加一个 FA_OPEN_EXISTING |
因为可能如果不加这个,就会每次都新建一个这个文件
é«éåå ¥CSVä¸XLSè¡¨æ ¼æµè¯DEMOéé¾æ¿.zip
2017-6-12 11:12 上传
点击文件名下载附件
下载积分: ST金币 -13.51 MB, 下载次数: 1898, 下载积分: ST金币 -1
嗯嗯,我在官方的例程中试了试,关闭之后能写进去,但是这段程序一到我自己的工程中就不行了,不知道哪儿出了点问题,我再看看。
以只写的方式打开,是先把内容清空,然后再写吧。PC上是这样,FATFS 上不知道是不是这样,我是没有遇到 过。
我是这么做的:
fr = f_open(&file, pathbuf, FA_OPEN_EXISTING|FA_WRITE|FA_READ);
连续打开写入关闭 30次不会有问题
/*********************************************************************************
向指定文件中添加一条记录
返回值: 0 成功,1 失败
*********************************************************************************/
u8 write_record(record_t *record, u8 wtype, u8 n)
{
u8 res = 0;
char *path="0:";
char pathbuf[48];
//float dat;
u32 wr;
FRESULT fr;
FIL file;
if( (record->date.year < 2017 || record->date.year > 2026) ||
(record->date.mon < 1 || record->date.mon > 12) ||
(record->date.day < 1 || record->date.day > 31))
{
Printf("ERROR: write_record 记录日期错误\r\n");
return 2;
}
memset(pathbuf, 0, sizeof(pathbuf));
switch(wtype)
{
case RECORD_TYPE_YEAR: //年记录
sprintf(pathbuf, "%s/%d/year.re", path, record->date.year);
break;
case RECORD_TYPE_MON:
sprintf(pathbuf, "%s/%d/%d/mon.re", path,record->date.year, record->date.mon);
break;
case RECORD_TYPE_DAY:
sprintf(pathbuf, "%s/%d/%d/day%d.re", path, record->date.year, record->date.mon, record->date.day);
break;
default:
break;
}
//return 0;
fr = f_open(&file, pathbuf, FA_OPEN_EXISTING|FA_WRITE|FA_READ);
if (fr == FR_OK)
{
Printf("f_open %s OK \r\n", pathbuf );
//写入内容 -- 24小时,第0条写入日累计数据,共25条数据
switch(wtype)
{
case RECORD_TYPE_YEAR: //年记录
#if 1
f_lseek(&file, n*4);
fr = f_write(&file, &record->yearflow[n], 4, &wr);
if (fr == FR_OK)
{
Printf("year: write_record %d,%.2f, %d byte \r\n", n,record->yearflow[n], wr);
res = 0;
}
else
{
Printf("write_record failed(year)!!\r\n");
res = 4;
}
#else //PC上需要这样做
dat = record->yearflow[n];
fread(record->yearflow , sizeof(record->yearflow[0]) , 13 , pFile );
record->yearflow[n] = dat;
fwrite(&record->yearflow[n] , sizeof(record->yearflow[n]) , 13 , pFile );
#endif
break;
case RECORD_TYPE_MON: //月记录
#if 1
f_lseek(&file, n*4);
fr = f_write(&file, &record->monflow[n], 4, &wr);
if (fr == FR_OK)
{
Printf("mon: write_record %d,%.2f, %d byte \r\n", n,record->monflow[n], wr);
res = 0;
}
else
{
Printf("write_record failed(mon)!!\r\n");
res = 3;
}
#else //PC上需要这样做
dat = record->monflow[n];
fread(record->monflow , sizeof(record->monflow[0]) , 32 , pFile );
record->monflow[n] = dat;
fwrite(record->monflow , sizeof(record->monflow[0]) , 32 , pFile );
#endif
break;
case RECORD_TYPE_DAY:// 日记录
#if 1
f_lseek(&file, n*4);
fr = f_write(&file, &record->dayflow[n], 4, &wr);
if (fr == FR_OK)
{
Printf("day: write_record %d,%.2f, %d byte \r\n", n,record->dayflow[n], wr);
res = 0;
}
else
{
Printf("write_record failed(day)!!\r\n");
res = 2;
}
#else //PC上需要这样做
dat = record->dayflow[n];
fread(record->dayflow , sizeof(record->dayflow[0]) , 25 , pFile );
record->dayflow[n] = dat;
fwrite(record->dayflow , sizeof(record->dayflow[0]) , 25 , pFile );
#endif // 0
break;
default:
res = 5;
break;
}
f_close(&file);
}
else
{
Printf("f_open %s failed\r\n", pathbuf);
res = 1;
}
return res;
}
试了试,不是这的问题,如上楼所言,我在别的工程里是没有问题的,不知道问什么在现在的工程里就有问题,还得再找找是怎么回事。
请问在实际应用中open,write,sync与close怎么使用,现在需要持续的写文件,但是不断的open操作及sync操作太占资源,导致别的任务出错,所以选择只open一次,然后不断的写,这样会有个问题,什么时候close文件实现保存。想了解下大家在实际工程中是怎么操作的