Hi
I have a program
int main(int srgc, char *argv[ ])
{
for(int i=1; i<50; i++)
{
system("dd if=/dev/zero of=file$i bs=1024 count=$i");
}
return 0;
}
My doubt is how to use the "$i" value inside C code
Please help
Try something like:
#include <stdio.h>
#include <stdlib.h>
int main(int srgc, char *argv[ ])
{
char cmd[64];
int i;
for(i = 1; i < 50; i++) {
sprintf(cmd, "dd if=/dev/zero of=file%d bs=1024 count=%d", i, i);
system(cmd);
}
return 0;
}
Obviously, you should be checking the return codes from sprintf() and system() for errors. And, unless you're using sprintf() to produce strings where you know that the output will never be longer than your buffer, you should use snprintf() instead. But, for demonstration purposes, I hope this helps.
2 Likes