1

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?

Salahuddin
  • 1,617
  • 3
  • 23
  • 37
  • 1
    Check this: https://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output – Fabio_MO Mar 20 '18 at 11:44
  • 1
    Thanks @Fabio_MO for your quick reply. This answers one of my questions. Is it the best way though to do the task? – Salahuddin Mar 20 '18 at 11:51
  • 3
    Better put the commands into a (Bash) script, and only execute that script from your C program. That way you can modify the script to adapt to changes (block device name, added error checking, etc.) without having to recompile your C code. If you use `popen(path_to_script, "w")`, you can supply the data to be written/appended to the script. – Nominal Animal Mar 20 '18 at 11:53
  • @SalahuddinAshraf the best way is suggested by (Nominal Animal), use "popen" only to check the output of your commands – Fabio_MO Mar 20 '18 at 11:57

0 Answers0