سؤال

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#define MAXLINIE 100


main(int argc, char* argv[]) {
    if (fork()==0){

        execl("/bin/> temporar.txt", "/bin/> temporar.txt", ">temporar.txt", NULL);

    }
}

Basically, what I am trying to do is creating a file using a process in unix, here is my code, but for some reason it does not work, I do not really understand the execl command and why the first two parameters have to be the same: execl("/bin/ls", "/bin/ls", "-l", NULL); this is working well, could someone help me ?

Thanks a lot!

هل كانت مفيدة؟

المحلول

first search whereis is touch:

~$ whereis touch
touch: /bin/touch /usr/bin/touch /usr/bin/X11/touch 

use: int execl(const char *path, const char *arg, ...);

execl("/bin/touch", "touch", "filename", NULL);
          ^            ^         ^         ^
       command       command   argument    
        path           name
                     arg 0     arg 1    

نصائح أخرى

Consider use system() instead:

system("/bin/ls -l > temporar.txt");

Or using execl call /bin/sh to redirect stream:

execl("/bin/sh", "/bin/sh", "-c" , "/bin/ls -l >temporar.txt", NULL);

First parameter of execl is a command to execute, second is a first parameter to be passed to command (argv[0]), third and next - other arguments argv[1] ...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top