如何在cpp中嵌入linux命令示例ln-l path1 path2



im试图创建一个指向/appdata/config/hello.txt到/appdata/debug/的路径的符号链接

std::string srcpath = "/appdata/config/hello.txt";
std::string debug = "/appdata/debug/";

在cpp代码中,我给出了ln -s srcpath debug

我甚至试过if(symlink(destPath.c_str(), DEBUG_PATH.c_str()) == 0)

但运气不好。

您还需要指定目标链接名称,例如:

std::string fileName = "hello.txt";
std::string srcpath = "/appdata/config/" + fileName;
std::string tgtpath= "/appdata/debug/" + fileName;
if (symlink(srcpath.c_str(), tgtpath.c_str()) != 0) {
std::cerr<< strerror(errno) << std::endl; // #include <errno.h> and <cstring> for this
}

按照您编写的方式,程序将输出一个";文件存在";错误消息,因为您正在将现有目录定义为链接目标。

最新更新