socket编程中的一些问题(2)

本文介绍了一种通过执行shell命令来检查特定路径是否已被挂载到文件系统的方法。使用cat /proc/mounts结合awk筛选出指定路径的挂载信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cat /proc/mounts //显示已加载的文件系统

mountPath //你自己定义的挂载路径

string cmd ="cat /proc/mounts | awk -F' ' '$2==\"" + mountPath + "\"'"; //判断mountPath是否挂载上

string result=ExecuteCommand(cmd);

//执行命令
string ExecuteCommand(const string command)
{
    FILE *fp;
    char *buffer = (char *)malloc(1024*1024);
    string result;
    if (buffer == NULL)
    {
        cout << "ExecuteCommand error : Memory allocation failure." << endl;
        return "";
    }

    memset(buffer,0,1024*1024);
    fp = popen(command.data(),"r");

    if (fp != NULL)
    {
        int size = fread(buffer,1,1024*1024,fp);
        if (size > 0)
        {
            result = buffer;
        }
        else
        {
            result = "";
        }

        pclose(fp);
        free(buffer);
    }
    else
    {
        cout << "the command maybe error!" << endl;
        free(buffer);
        result = "";
    }
    return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值