gpt4 book ai didi

c++ - 写入内核配置时为 "Not a directory"

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:11 28 4
gpt4 key购买 nike

我正在尝试使用 Qt 中的这个函数在 Linux 系统上切换 IPv6。问题是它无法打开文件,只是报告“不是目录”。

bool toggle_ipv6(const bool &enabled) {
const std::vector<std::string> ipv6_kernel_option_files = {
"/proc/sys/net/ipv6/conf/all/disable_ipv6"
"/proc/sys/net/ipv6/conf/default/disable_ipv6"
"/proc/sys/net/ipv6/conf/lo/disable_ipv6"
};

for (const auto &filename: ipv6_kernel_option_files) {
QFile kernel_option_file( filename.c_str() );
if ( kernel_option_file.open(QIODevice::WriteOnly) ) {
QTextStream stream(&kernel_option_file);
stream << (enabled ? "0" : "1");
kernel_option_file.close();
} else {
const std::string error_message = kernel_option_file.errorString().toStdString();
qDebug().nospace().noquote() << '[' << QTime::currentTime().toString() << "]: " << error_message.c_str();
return false;
}
}

return true;
}

我试过在网上搜索,但找不到与 QFile 和此特定错误消息有关的任何其他问题。我该如何解决这个问题?

最佳答案

vector 初始化中缺少逗号:

const std::vector<std::string> ipv6_kernel_option_files = {
"/proc/sys/net/ipv6/conf/all/disable_ipv6"
"/proc/sys/net/ipv6/conf/default/disable_ipv6"
"/proc/sys/net/ipv6/conf/lo/disable_ipv6"
};

因此 vector 只有一个元素,它是由三个路径连接而成的字符串:

"/proc/sys/net/ipv6/conf/all/disable_ipv6/proc/sys/net/ipv6/conf/default/disable_ipv6/proc/sys/net/ipv6/conf/lo/disable_ipv6"

考虑到这一点

"/proc/sys/net/ipv6/conf/all/disable_ipv6"

是一个文件,不是一个目录,它不能包含路径的其余部分。

在 vector 初始化中使用逗号分隔路径:

const std::vector<std::string> ipv6_kernel_option_files = {
"/proc/sys/net/ipv6/conf/all/disable_ipv6",
"/proc/sys/net/ipv6/conf/default/disable_ipv6",
"/proc/sys/net/ipv6/conf/lo/disable_ipv6"
};

关于c++ - 写入内核配置时为 "Not a directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com