本帖最后由 zsz2008610 于 2013-6-8 10:46 编辑
在fatfs文件系统下,实现SD卡的读取追加内容,实现方法是:读取文件、偏移指针、填写内容。追加内容时需要利用f_sleek函数将指针移到文本尾部,再写问容,具体实现main函数的功能是两个按键来控制添加不同的内容,按键1按下时添加 BYTE sample_buffer[] = "2710,2712,2711,2715,1234,2134,4567,7890
";按键2按下时添加 BYTE sample_buffer1[] = "1,2,3,4,5,6,7,8,9,0
";
printf 调试用,
问题是:保存到txt文件中的数据不规则,应该是每一行数据都是靠左边对齐才对,实际是如附件图片所示。
程序:
while (1)
{
if( Key_Scan(GPIOE,GPIO_Pin_5) == KEY_ON)//按键1按下时添加 sample_buffer中内容
{
printf( "
KEY1 PE5
" );//串口输出调试信息
/* 将采样数据的数据写到文件中 先打开,打开成功后再追加文本*/
res = f_open(&fdst,"0:/Demo.TXT", FA_WRITE);//打开文本,while之前已经 f_mount(0,&fs);
if( res == FR_OK)
{
printf( "
SD has opened
" );
/* Move to end of the file to append data */
res = f_lseek(&fdst, f_size(&fdst));
res = f_write(&fdst, sample_buffer, sizeof(sample_buffer), &bw);
f_close(&fdst);
printf( "
sample_buffer
" );
}
}
if( Key_Scan(GPIOE,GPIO_Pin_6) == KEY_ON) //按键2按下时添加 sample_buffer1中内容
{
printf( "
KEY2 PE6
" );
/* 将采样数据的数据写到文件中 */
res = f_open(&fdst,"0:/Demo.TXT", FA_WRITE);
if( res == FR_OK)
{
printf( "
SD has opened
" );
/* Move to end of the file to append data */
res = f_lseek(&fdst, f_size(&fdst));
res = f_write(&fdst, sample_buffer1, sizeof(sample_buffer), &bw);
f_close(&fdst);
printf( "
sample_buffer1
" );
}
}
}
试验结果:txt文本内容
实际显示内容对齐方式是:
感谢您选用 野火STM32开发板 !^_^
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
2710,2712,2711,2715,1234,2134,4567,7890
2710,2712,2711,2715,1234,2134,4567,7890
2710,2712,2711,2715,1234,2134,4567,7890
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
理论或是理想中的显示内容对齐方式是:
感谢您选用 野火STM32开发板 !^_^
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
2710,2712,2711,2715,1234,2134,4567,7890
2710,2712,2711,2715,1234,2134,4567,7890
2710,2712,2711,2715,1234,2134,4567,7890
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
1,2,3,4,5,6,7,8,9,0
一周热门 更多>