Skip to content
Advertisement

How to create hard link in Linux from a C program

We know we can create hard link in Linux using ln file1 file2 which will make file2 a hard link of file1.

However when I try to do this by using a C program, I face issues. Below is the C code.

JavaScript

After compiling this program with gcc I run it as below.

JavaScript

Here file1 exists and file2 is the desired hardlink.

Could anyone point where did I make mistake here.

Thanks.

Advertisement

Answer

As per the test input shown by you

JavaScript

in your code

JavaScript

should read

JavaScript

That said, it is always better and advisable to use the argv[n] after checking argc against n+1.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement