Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years ago.
Increment File names if they are already present in the file system.
This is my first program. with mbed, and my experience is with C as i am an electrical engineer and do not have much programing experiance.
Anyway.
I am eventualy creating a stand alone CAN data logging system that will have a small batter back and will desing a BMS system..
I have the USBMSD host set up from the handbook,
I want to check the file system for a peticualr file name incrementation, and if that file already exists, it will increment the next file.
Eventually this will be a time stamp, but for now i just want the filet to be name test1, test2, test3 and so on.
Everythign ive tried to do with my basic C knowledge has lead to many errors.
This is the base code.
- include "mbed.h"
- include "USBHostMSD.h"
DigitalOut led(LED1);
void msd_task(void const *) {
USBHostMSD msd("usb"); int i = 0;
while(1) {
try to connect a MSD device while(!msd.connect()) { Thread::wait(10); }
in a loop, append a file if the device is disconnected, we try to connect it again while(1) {
append a file FILE * fp = fopen("/usb/test1.txt", "a");
if (fp != NULL) { fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++); printf("Goodbye World!\r\n"); fclose(fp); } else { printf("FILE == NULL\r\n"); }
Thread::wait(10);
if device disconnected, try to connect again if (!msd.connected()) break; }
} }