Brgv
Brgv

Reputation: 21

how to embedded a linux command example ln -l path1 path2 in cpp

im trying to create a symlink to a path /appdata/config/hello.txt to /appdata/debug/

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

In cpp code I have given ln -s srcpath debug

I even tried if(symlink(destPath.c_str(), DEBUG_PATH.c_str()) == 0)

But no luck.

Upvotes: 0

Views: 72

Answers (1)

andreee
andreee

Reputation: 4679

You need to specify the target link name as well, e.g. like this:

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
}

The way you've written it the program will output an "File exists" error message, because you're defining an existing directory as link target.

Upvotes: 1

Related Questions