I'm using Ubuntu 16.10. I want to copy contents of a file to another file using execlp. I have two files named file1 (with content) and file2 (empty). If I give the following command on terminal
test@ubuntu:~/Teme$ cat file1 > file2
Everything works perfect, but if I write a C code and use this:
execlp("/bin/cat","cat","file1",">","file2",NULL);
I get the error:
content of file1
cat: '>': No such file or directory
I thought that execlp is taking cat arguments one by one so I tried
execlp("/bin/cat","cat","file1 > file2",NULL);
but then I got this error:
cat: 'file1 > file2': No such file or directory
How can I copy the content from a file to another using execlp / execl?
cat
does not copy between files -- that'scp
.cat
concatenates files to standard output.cat
as a command and then as the first parameter to itself:execlp("/bin/cat","cat","file1 > file2",NULL);
$0
/argv[0] is usually the name of the command. See unix.stackexchange.com/q/315812/70524