Mount USB drive in linux with C

2020-06-06 05:17发布

问题:

I am looking to programatically mount a USB drive in Linux, so that I can write a text document using fprintf. I am having trouble finding out how to mount the drive. I have been searching the web for an answer, and I found many tutorials of how to do it via the command line, but none in C. Can someone please point me in the right direction with this.

回答1:

man 2 mount

e.g.

#include <sys/mount.h>

if (mount("/dev/mmcblk0p1", "/mnt/sd", "vfat", MS_NOATIME, NULL)) {
    if (errno == EBUSY) {
        printf("Mountpoint busy");
    } else {
        printf("Mount error: %s", strerror(errno));
    }
} else {
    printf("Mount successful");
}


标签: c linux usb