Currently, I can save the file in the same directory of the project. I'd like to save some data in a text file and save this file in an SD card if any is inserted to my board (BeagleBone Black). Otherwise, if no card is inserted, the file should be saved to the current location.
I know that the following sequence is required to do this task directly in terminal:
# Check if some card is inserted under the usual name
fdisk -l | grep mmcblk0p1
# if something returned, then a card is inserted
sudo mkdir /media/sdcard/
sudo mount /dev/mmcblk0p1 /media/sdcard/
cd /media/sdcard/
touch data.txt
umount /dev/mmcblk0p1
I thought about calling these commands in my C program using system
function. But I don't know how to check if the first command has returned something or NULL.
I have two questions:
1- Is using the above commands is the best way to check if an SD card is inserted?
2- How to check the output of a command run by system
?