FRESULT res_mount; //定义FRESULT 类型的变量,用来接收挂载函数f_mount()的返回值
FRESULT res_mkfs; //定义初始化函f_mkfs()的返回值
FRESULT res_open;
FRESULT res_write;
FRESULT res_read;
USART_Config();
SPI_Flash_config();
printf("\r\n这是一个FatFs文件系统管理FLASH的实验\r\n");
res_mount = f_mount(&fsobject,"1:",1);
printf("\r\nf_mount res=%d",res_mount);
if(res_mount == FR_NO_FILESYSTEM) //挂载文件返回值为无文件系统时,进行格式化
{
printf("\r\n FLASH 还没有文件系统,即将进行格式化...\r\n");
res_mkfs = f_mkfs("1:",0,0);
printf("\r\nf_mkfs res=%d",res_mkfs);
if(res_mkfs == FR_OK)
{
printf("\r\n 《FLASH 已经成功格式换文件系统...》\r\n");
//格式化后要取消挂载后重新挂载
res_mount = f_mount(NULL,"1",1);
res_mount = f_mount(&fsobject,"1",1);
}
else
{
printf("《格式化失败》\r\n");
}
printf("\r\nsecond res_mount=%d",res_mount);
}
/*打开文件*/
res_open = f_open(&fp,"1:abcdfgfgrgrgdgd.txt",FA_OPEN_ALWAYS| FA_READ | FA_WRITE);
printf("\r\n fopen res=%d",res_open);
if(res_open == FR_OK) //文件打开成功
{
res_write = f_write(&fp,write_string,sizeof(write_string),&bw);
printf("\r\nbw=%d",bw);
printf("\r\n res_write=%d",res_write);
if(res_write == FR_OK)
{
f_lseek(&fp,0); //调整光标指针的位置 ,使其在文件开头 ,如果 宏_FS_MINIMIZE 配置为3f_lseek()函数将用不了
res_read = f_read(&fp,read_string,f_size(&fp),&br);
printf("\r\n res_read=%d",res_read);
if(res_read == FR_OK)
{
printf("\r\n读取到的数据是:%s,%d",read_string,br);
}
}
}
***********************************以上是我写的程序**************************************************
为什么串口调试助手显示 f_open 函数的返回值是 7??
是因为 函数 f_open 的打开模式有问题吗? 我是这样写的
res_open = f_open(&fp,"1:abcdfgfgrgrgdgd.txt",FA_OPEN_ALWAYS| FA_READ | FA_WRITE);
求大神指点,谢谢!! 自己调了好长时间,没有解决 |