|
测试代码:
- /*
- *********************************************************************************************************
- * 函 数 名: CreateNewFile
- * 功能说明: 在SD卡创建一个新文件,文件内容填写“www.armfly.com”
- * 形 参:无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- static void CreateNewFile(void)
- {
- /* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
- FRESULT result;
- FATFS fs;
- FIL file;
- DIR DirInf;
- uint32_t bw;
- /* 挂载文件系统 */
- result = f_mount(&fs, "0:", 0); /* Mount a logical drive */
- if (result != FR_OK)
- {
- printf("挂载文件系统失败 (%s)\r\n", FR_Table[result]);
- }
- /* 打开根文件夹 */
- result = f_opendir(&DirInf, "0:"); /* 如果不带参数,则从当前目录开始 */
- if (result != FR_OK)
- {
- printf("打开根目录失败 (%s)\r\n", FR_Table[result]);
- return;
- }
- /* 打开文件 */
- result = f_open(&file, "0:/armfly.txt";, FA_OPEN_ALWAYS | FA_WRITE);
- result = f_write(&file, "FatFS Write Demo \r\n www.armfly.com \r\n", 34, &bw);
- if (result == FR_OK)
- {
- printf("armfly.txt 文件写入成功\r\n");
- }
- else
- {
- printf("armfly.txt 文件写入失败 (%s)\r\n", FR_Table[result]);
- }
- f_close(&file);
-
- result = f_open(&file, "0:/armfly.txt", FA_OPEN_ALWAYS | FA_WRITE);
- f_lseek(&file, 34);
- result = f_write(&file, "FatFS Write Demo \r\n www.armfly.com \r\n", 34, &bw);
- if (result == FR_OK)
- {
- printf("armfly.txt 文件写入成功\r\n");
- }
- else
- {
- printf("armfly.txt 文件写入失败 (%s)\r\n", FR_Table[result]);
- }
- f_close(&file);
- /* 卸载文件系统 */
- f_mount(NULL, "0:", 0);
- }
复制代码
插入末尾的效果:
|
|