Reputation: 61
Im converting some of my old .pcd images to jpeg using the pcdtojpeg solution. Since I have a lot of pictures Im penning up a C program to automate the process. I have moderate C# experience but still new to C.
Here is what Im currently at with some placeholder magicnumbers:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i=0;
for (i=0; i<90; i++)
{
if(i<10)
{
system("sudo ./pcdtojpeg img000%d.pcd", i);
}
else
{
system("sudo ./pcdtojpeg img00%d.pcd", i);
}
}
return 0;
}
The compiler outputs an error defining too many arguments to the system function as it may not take any.
I've googled for plenty of documentation around the web like cplusplus.com and linux.die.net but haven't found any answers how to properly iterate inside the system() function in C.
I am running
GCC 4.8.0
Linux 3.9.2-1-ARCH #1 SMP PREEMPT x86_64 GNU/Linux
Upvotes: 0
Views: 246
Reputation: 70243
Seconding Maxim Skurydin's comment here.
Your C code is a simple loop around shell calls.
In this case, C is the wrong technology to use. Use a shell script instead, and lose the sudo
because you should never have to use that except for system administration purposes:
for id in $(seq -f %04.f 0 89)
do
./pcdtojpeg img${id}.pcd
done
Quicker done, much easier to comprehend and update if necessary.
(ShinTakezou did beat me to an even simpler solution, if the files you want to touch can be collected with a pattern-matching ls
.)
Upvotes: 4
Reputation: 399753
You don't want to "iterate inside system()
". What you want to do, is iterate around system()
, so that you call it multiple times. Your code already does this. The missing part is how to format the command to be different on each call.
For this, you should use snprintf()
:
char buf[1024];
snprintf(buf, sizeof buf, "sudo ./pcdtojpeg img%03d.pcd", i);
system(buf);
The process of using %
-codes to format a string is not something that is built into the C language like your code seems to expect. It's simply a convention implemented by a set of (library) functions, like printf()
and snprintf()
.
The %03d
formatting code means "insert a decimal number from an int
, and pad it using zeroes on the left so that the width becomes 3 digits, total". Thus, no if
is needed for this.
Upvotes: 4