I have inherited a project that logs data from a single board computer to a usb flash drive. The code that does this is written in C. The old code is:
//Get the Start time
sttime = localtime(&starttime);
//Get the name of the drive
sprintf(fname,"/mnt/SJ-HMIData%.4i%.2i%.2i%.2i%.2i%.2i.sjrd",(*sttime).tm_year+1900,(*sttime).tm_mon+1,(*sttime).tm_mday,(*sttime).tm_hour,(*sttime).tm_min,(*sttime).tm_sec);
//Open an IO port with read only
UsbFile = open(fname, O_CREAT|O_WRONLY|O_TRUNC);//Open csv on flash drive to be appended
My problem is that this relies on the time I insert the flash drive, as being the time that this program runs. How can I make it so that I don’t need the time to open the device?
I suspect the date/time have nothing to do with the mounting. Most likely the flash drive is being mounted at /mnt
The date and time is being used to assemble the name for a file to write on the mounted filesystem. If you do this periodically, it’s actually pretty common practice to put some sort of timestamp in the file name – it’s more convenient to see, and more reliable than the filesystem date fields which might be accidentally lost or if the file is moved to another system or storage medium before being analyzed.
It doesn’t actually seem like you should be having a problem here. If you want to make a file in that style, go with the current code (it would be good to at least update the comments to what it does now, which looks to be different than what it did in the version where they were written). If you want to make a file by a different time-invariant name, then do so.
Perhaps your flash drive is not getting automatically mounted (perhaps it doesn’t have the expected filesystem on it, or some daemon that was supposed to handle this isn’t working). That could well be the case, but the code you have posted is not involved in the mounting or failure to mount. Instead, it’s code that attempts to write to what is presumably an already mounted filesystem, or failing that, into the directory of the parent filesystem that was supposed to be used as a mount point…