Can a call to system be string variable or actual string in double quoted form?
Code:sprintf(cmd, "cp %d.txt %d.csv",i,i);
pid_err=system(cmd); //Problem here: it's printing garbage on the screen
Printable View
Can a call to system be string variable or actual string in double quoted form?
Code:sprintf(cmd, "cp %d.txt %d.csv",i,i);
pid_err=system(cmd); //Problem here: it's printing garbage on the screen
>Can a call to system be string variable or actual string in double quoted form?
Either of them is acceptable. Fo instance
>Problem here: it's printing garbage on the screenCode:char str[] = "clear";
system(str);
or
system("clear");
If the return value was other than 0 then the system function failed.
ssharish
I am using gdb to walk through the break points.
printf("print before system(cmd) call\n");
pid_err=system(cmd); //Problem here: it's printing garbage on the screen
printf("print after system(cmd) call\n");
Soon as I call system(cmd), garbage is printed out on the screen. why?
sample output:
Quote:
��ႇ
.x��ႇ
.x��ႇ
.x��ႇ
�<\�p��<\�p�
Check the system() return value and see if your system call was successful.
ssharishCode:if( system("clear") )
{
printf( " Error " );
return 1;
}
else
printf( " < Message > " );
I don't know what the problem here. I tried to gzip the file using system(). Text file is there, but never get gzipped. I get garbage printed out on the console soon as I call system();.
Do I have to have root access on the linux shell to be able to call system? Does
sprintf(cmd, "gzip -c ./data_id_%d.txt",i); put a '\0' in cmd, after placing the string "gzip -c ./data_id_%d.txt"?
server.conf:
file_temp.c:Code:num_of_files=3;
Here is my gdb output.Code:#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 256
FILE *configfile;
int num_files;
char conf[100];
char *buffer;
char cmd[SIZE];
int main (int argc, char *argv[])
{
int i=0;
configfile= fopen("./server.conf", "r");
fgets(conf,100, configfile);
fclose(configfile);
buffer=strtok(conf,";");
strtok(buffer,"=");
num_files=atoi(strtok(NULL,"="));
sprintf(cmd, "gzip -c ./data_id_%d.txt",i);
printf("system(%s)\n", cmd);
if( system(cmd) )
{
printf( " Error \n" );
return 1;
}
else
printf( " < System call success > \n" );
printf("After the system call\n");
exit (0);
}
Code:****printted whole bunch of garbage here.********* and then stuff below*********
System call has success >
Breakpoint 2, main () at file_temp.c:34
34 printf("After the system call\n");
(gdb) continue
Continuing.
After the system call
Program exited normally.
Is this you as well?
http://www.daniweb.com/forums/thread101796.html
Salem, I tried fork as you mentioned on Daniweb. It gave a problem. I wanted to try system call. This is also giving me problems.
Can someone help with this? No one had done zipping stuff from system call?
Anything else I need to do, i.e setuid?
million thanks for everyone.