confucius_007
confucius_007

Reputation: 326

usage of _execl() function on Windows10 platform

_execl() is returning -1 and error message as "No such file or directory" even though the given file is there. When I run gzip command directly on command prompt it works. I am not able to understand what is it that I am missing here.

#include <stdio.h>
#include <process.h>
#include <errno.h>

void main(){
int ret = _execl("cmd.exe", "gzip.exe", "C:\\Users\\user_name\\work\\Db618\\test.txt");
printf("ret: %d \t strerror: %s\n", ret, strerror(errno));
}

Can someone give an example of how to use this function, I found one more API system() while looking for a solution, but before using that I wanted to know what is the difference in both of these on Windows platform?

Upvotes: 0

Views: 561

Answers (1)

Zeus
Zeus

Reputation: 3890

According to the _execl:Your first parameter does not need to be cmd.exe, but should be the first command of the command line, like gzip.exe.

You can refer to the MSDN sample.

Finally, your program only needs to delete the initial "cmd.exe", but it should be noted that the last parameter must be NULL to indicate termination.

Here is the code:

#include <stdio.h>
#include <process.h>
#include <errno.h>
#include <cstring>

int main(int argc, const char* argv[])
{
    int ret = _execl("D:\\gzip-1.3.12-1-bin\\bin\\gzip.exe" ,"-f","D:\\gzip-1.3.12-1-bin\\bin\\test.txt" ,NULL);
    printf("ret: %d \t strerror: %s\n", ret, strerror(errno));
    return 0;
}

If you want to use system, you can pass the command as a parameter to the system function just like using CMD to achieve the same effect.

You can use it like:

system("gzip.exe test.txt");

Upvotes: 1

Related Questions