在C/C++中,可以使用以下方法来检查文件系统是否已挂载:
#include <sys/statvfs.h>
#include <stdio.h>
int main() {
struct statvfs fs_info;
const char* mount_point = "/path/to/mount/point";
if (statvfs(mount_point, &fs_info) == 0) {
printf("File system is mounted.\n");
} else {
printf("File system is not mounted.\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_mounted(const char* mount_point) {
FILE* fp;
char line[256];
char* token;
fp = popen("mount", "r");
if (fp == NULL) {
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL) {
token = strtok(line, " ");
if (token != NULL && strcmp(token, mount_point) == 0) {
pclose(fp);
return 1;
}
}
pclose(fp);
return 0;
}
int main() {
const char* mount_point = "/path/to/mount/point";
if (is_mounted(mount_point)) {
printf("File system is mounted.\n");
} else {
printf("File system is not mounted.\n");
}
return 0;
}
以上两种方法都可以用来检查文件系统是否已挂载。根据实际需求选择适合的方法即可。
注意:以上示例代码仅为演示目的,实际使用时需要根据具体情况进行适当的错误处理和参数校验。
领取专属 10元无门槛券
手把手带您无忧上云