基于正点原子的库函数版本的FATS文件系统,实现向文件写入字符串,读取文件内容,这里是指txt文件,其余文件其实也一样,读取成字节。
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "key.h"
#include "MMC_SD.h"
#include "ff.h"
#include "exfuns.h"
#include "fattester.h"
#include "malloc.h"
#include "string.h"
extern FATFS *fs[2];
extern FIL *file; //文件1
extern UINT br, bw; //读写变量
// 将字符串写入txt文件
void write_to_txt(const char *filename, const char *str) {
FRESULT res;
res = f_open(file, filename, FA_CREATE_ALWAYS | FA_WRITE); // 打开或创建文件
if (res != FR_OK) {
printf("Failed to open file: %d\r\n", res);
return;
}
res = f_write(file, str, strlen(str), &bw); // 写入数据
if (res != FR_OK) {
printf("Failed to write file: %d\r\n", res);
f_close(file);
return;
}
res = f_close(file); // 关闭文件
if (res != FR_OK) {
printf("Failed to close file: %d\r\n", res);
return;
}
printf("Write successfully, %u bytes written\r\n", bw);
}
void append_to_file(const char *filename, const char *data) {
FRESULT res;
res = f_open(file, filename, FA_WRITE | FA_OPEN_ALWAYS); // 打开文件
if (res != FR_OK) {
printf("Failed to open file: %d\r\n", res);
return;
}
res = f_lseek(file, file->fsize); // 将写入指针定位到文件末尾
if (res != FR_OK) {
printf("Failed to seek file: %d\r\n", res);
f_close(file);
return;
}
res = f_write(file, data, strlen(data), &bw); // 写入数据到文件末尾
if (res != FR_OK || bw == 0) {
printf("Failed to write file: %d\r\n", res);
f_close(file);
return;
}
res = f_close(file); // 关闭文件
if (res != FR_OK) {
printf("Failed to close file: %d\r\n", res);
return;
}
}
#define MAX_LINE_SIZE 100
// 动态读取txt文件的行并进行处理
void process_lines_from_txt(const char *filename) {
FRESULT res;
char line[MAX_LINE_SIZE];
res = f_open(file, filename, FA_READ); // 打开文件
if (res != FR_OK) {
printf("Failed to open file: %d\r\n", res);
return;
}
while (f_gets(line, MAX_LINE_SIZE, file) != NULL) { // 按行读取文件内容
printf("%s", line); // 打印读取的内容
// 这里可以对每一行进行处理,例如调用其他函数进行处理
// 读取下一行之前可以进行必要的等待或延时操作
}
res = f_close(file); // 关闭文件
if (res != FR_OK) {
printf("Failed to close file: %d\r\n", res);
return;
}
}
int main(void) {
char str[] = "Hello, world!";
char str1[] = "\r\nHello, world!";
u32 total, free;
u8 t = 0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
delay_init(); //延时函数初始化
uart_init(115200); //串口初始化为9600
exfuns_init(); //为fatfs相关变量申请内存
LED_Init(); //LED初始化
mem_init(); //初始化内存池
while (SD_Initialize()) //检测SD卡
{
delay_ms(200);
LED0 = !LED0;//DS0闪烁
}
exfuns_init(); //为fatfs相关变量申请内存
f_mount(fs[0], "0:", 1); //挂载SD卡
f_mount(fs[1], "1:", 1); //挂载FLASH.
while (exf_getfree("0", &total, &free)) //得到SD卡的总容量和剩余容量
{
delay_ms(200);
LED0 = !LED0;//DS0闪烁
}
//打印大小
printf("SD卡容量:%d MB\r\nSD卡剩余容量:%d MB\r\n", total / 1024, free / 1024);
//write_to_txt("text.txt", str); // 写入字符串到txt文件
//append_to_file("text.txt", str1); // 写入字符串到txt文件
process_lines_from_txt("text.txt");
while (1) {
t++;
delay_ms(200);
LED0 = !LED0;
}
}
支持的文件列表:
//文件类型列表
const u8 *FILE_TYPE_TBL[6][13]=
{
{"BIN"}, //BIN文件
{"LRC"}, //LRC文件
{"NES"}, //NES文件
{"TXT","C","H"}, //文本文件
{"MP1","MP2","MP3","MP4","M4A","3GP","3G2","OGG","ACC","WMA","WAV","MID","FLAC"},//音乐文件
{"BMP","JPG","JPEG","GIF"},//图片文件
下图是读取到的数据展示: