bash - C - What is a proper way to loop inside a system() function? -


im converting of old .pcd images jpeg using pcdtojpeg solution. since have lot of pictures im penning c program automate process. have moderate c# experience still new c.

here im @ placeholder magicnumbers:

#include <stdio.h> #include <stdlib.h>  int main(int argc, char *argv[]) { int i=0;  (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 error defining many arguments system function may not take any.

i've googled plenty of documentation around web cplusplus.com , linux.die.net haven't found answers how iterate inside system() function in c.

i running

gcc 4.8.0

linux 3.9.2-1-arch #1 smp preempt x86_64 gnu/linux

you don't want "iterate inside system()". want do, iterate around system(), call multiple times. code this. missing part how format command different on each call.

for this, should use snprintf():

char buf[1024];  snprintf(buf, sizeof buf, "sudo ./pcdtojpeg img%03d.pcd", i); system(buf); 

the process of using %-codes format string not built c language code seems expect. it's convention implemented set of (library) functions, printf() , snprintf().

the %03d formatting code means "insert decimal number int, , pad using zeroes on left width becomes 3 digits, total". thus, no if needed this.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -