Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue passing optional parameters into C execlp()

I’m writing a C program that uses execlp() to run the linux command-line tool, convert. This command takes optional arguments. However, when using it with execlp(), my C program doesn’t recognize the flags I pass in and thus doesn’t do the command properly.

For example, if I were to run this command in terminal convert -resize 10% src.jpg dst.jpg it will resize the src image by 10%, saving it to dst. However when I run my C program with this code

rc = execlp("convert", "-resize 10%", src, dst, NULL);

my computer doesn’t recognize the resize -10% flag and doesn’t do anything to my source image. Why is that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By convention, the first parameter to a process (accessible as argv[0]) is the name of the process. You haven’t included that, so "-resize 10%" is read as the process name instead of an option.

Also, "-resize 10%" is actually two parameters separated by a space, so you need to split them up.

rc = execlp("convert", "convert", "-resize", "10%", src, dst, NULL);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading